我正在使用 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);
}
}