0

我正在使用 javax 邮件类通过 Gmail SMTP 发送多封带有附件的电子邮件。电子邮件发送是在 doInBackground() 函数中的 AsyncTask 内完成的。我有一个邮件数组列表,我正在逐一发送。第一封电子邮件成功发送,但从下一封电子邮件中我收到了 MessagingException:无法连接到 SMTP 主机。我不太确定为什么会出现这个问题。是否可能没有释放所需的资源?有谁知道这个问题的解决方案?

04-30 10:10:25.109: W/System.err(619): javax.mail.MessagingException: 无法连接到 SMTP 主机:localhost,端口:25;04-30 10:10:25.109: W/System.err(619): 嵌套异常是: 04-30 10:10:25.109: W/System.err(619): java.net.ConnectException: localhost/127.0。 0.1:25 - 连接被拒绝 04-30 10:10:25.109: W/System.err(619): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391) 04-30 10:10: 25.109: W/System.err(619): 在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412) 04-30 10:10:25.109: W/System.err(619): 在 javax .mail.Service.connect(Service.java:288) 04-30 10:10:25.109: W/System.err(619): 在 javax.mail.Service.connect(Service.java:169) 04-30 10 :10:25.109: W/System.err(619): 在 javax.mail.Service.connect(Service.java:118) 04-30 10:10:25.109: W/System.err(619): 在 javax。邮件。

我的发送电子邮件异步任务类

class SendEmailAsyncTask extends AsyncTask <Void, Void, Boolean> {
    Mail thisMail;

    public SendEmailAsyncTask() {
        if(V) Log.v(SendEmailAsyncTask.class.getName(), "SendEmailAsyncTask()");
        this.thisMail = mailQueue.get(mailCounter);
        sendMessageToClient("Sending " + thisMail.getRecipients());

    }

    @Override
    protected Boolean doInBackground(Void... params) {
        if(V) Log.v(SendEmailAsyncTask.class.getName(), "doInBackground()");
        try {
            thisMail.send();
            return true;
        } catch (AuthenticationFailedException e) {
            Log.e(SendEmailAsyncTask.class.getName(), "Gmail account details are wrong");
            createNotification("Gmail account details are incorrect. Please check them in the settings.");
            setFlag(FLAG_BAD_ACCOUNT);
            e.printStackTrace();
            return false;
        } catch (MessagingException e) {
            Log.e(SendEmailAsyncTask.class.getName(), thisMail.getRecipients() + " sending failed");
            e.printStackTrace();
            return false;
        } 
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if(result) { // email send success
            //sent successfully 
            if(D) Log.d(SendEmailAsyncTask.class.getName(), "email sent successfully");
            sendMessageToClient("Email sent");
        } else {
            //retry? or quit
            if(D) Log.d(SendEmailAsyncTask.class.getName(), "email was not sent");
            sendMessageToClient("Email not sent");
        }       

            mailCounter++;
            // if there are more mails in the queue start another send email task
            if (mailCounter < mailQueue.size()) {
                SendEmailAsyncTask sendTask = new SendEmailAsyncTask();
                sendTask.execute();

            // if there are no more in the mail queue, delete the data set
            } else {
                deleteTask = new DeleteFilesAsyncTask(currentDataSet.getDate(), currentDataSet.getMachine());
                deleteTask.execute();
            }

        sendTask = null;
    }
}

编辑:查看我的邮件包装类,我使用的端口是 465,而不是 25。我想知道这是否意味着什么?

public class Mail extends javax.mail.Authenticator {   
private String mailhost = "smtp.gmail.com";   
private String user;   
private String password;   
private Session session;   
private Multipart multipart;
private String subject;
private String body;
private String sender;
private String recipients;

static {   
    Security.addProvider(new JSSEProvider());   
}  

public Mail(String user, String password) {
    this.user = user;   
    this.password = password;   
    multipart = new MimeMultipart();

    Properties props = new Properties();   
    props.setProperty("mail.transport.protocol", "smtp");   
    props.setProperty("mail.host", mailhost);   
    props.put("mail.smtp.auth", "true");   
    props.put("mail.smtp.port", "465");   
    props.put("mail.smtp.socketFactory.port", "465");   
    props.put("mail.smtp.socketFactory.class",   
            "javax.net.ssl.SSLSocketFactory");   
    props.put("mail.smtp.socketFactory.fallback", "false");   
    props.setProperty("mail.smtp.quitwait", "false");   

    //session = Session.getDefaultInstance(props, this);
    session = Session.getInstance(props, this);
}   

}

4

1 回答 1

1

好吧,因为似乎没有人有答案......我会回答我自己的问题。

我通过在创建电子邮件后立即发送电子邮件而不是稍后安排发送来解决问题。

以前的代码:(一次成功发送后出错)

class CreateEmailAsyncTask extends AsyncTask <Void, Void, Boolean> {
    @Override
    protected Boolean doInBackground(Void... params) {
        // create emails and add to the queue
    }

    @Override
    protected void onPostExecute(Boolean result) {
        SendEmailAsyncTask sendTask = new SendEmailAsyncTask();
        sendTask.execute();
}

class SendEmailAsyncTask extends AsyncTask <Void, Void, Boolean> {
    @Override
    protected Boolean doInBackground(Void... params) {
        // send emails
    }
}

更改的代码:(适用于所有电子邮件)

class CreateEmailAsyncTask extends AsyncTask <Void, Void, Boolean> {
    @Override
    protected Boolean doInBackground(Void... params) {
        // create emails
        // send emails
    }
}

我认为可能存在超时或使邮件对象无效的东西,但我不确定。我检查了Java Mail API 文档,但找不到太多信息...

于 2012-04-30T22:40:02.277 回答