【发布时间】:2018-06-11 13:36:11
【问题描述】:
我们有一个应用程序严重依赖 JAVA Mail 来发送电子邮件。我们已经按照以下设置了连接超时属性(从 Long 值设置,是否需要整数才能生效?):
props.put("mail.smtp.timeout", 1000L);
props.put("mail.smtp.connectiontimeout", 1000L);
一段时间后,应用程序停止并且永远无法从 Office 365 smtp 帐户恢复(仅在 Office 365 中发生)。我们在 JAVA 邮件中启用了调试模式,它失败的行如下:
DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp-mail.outlook.com", port 587, isSSL false
当达到这一点时,套接字超时似乎不起作用,应用程序停止。下面是一个无限循环,不断连接到邮件服务器,然后最终卡住。
public static void main(String[] args){
String smtpServer = "smtp-mail.outlook.com";
String username = "test@domain.com";
String password = "password";
int portNumber = 587;
Long socketTimeout = 10000L;
Properties props = new Properties();
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", portNumber);
props.put("mail.smtp.timeout", socketTimeout);
props.put("mail.smtp.connectiontimeout", socketTimeout);
props.put("mail.smtp.starttls.enable", "true");
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
while(true){
Session sess = Session.getInstance(props, authenticator);
sess.setDebug(true);
try {
Transport t = sess.getTransport("smtp");
t.connect(smtpServer, portNumber, username, password);
t.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
【问题讨论】:
标签: java office365 jakarta-mail