JAVA 邮件和 Office 365 在连接时挂起

JAVA Mail and Office 365 hanging upon connection

我们有一个严重依赖 JAVA 邮件发送电子邮件的应用程序。我们已按照以下设置连接超时属性(从 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();
        }
    }
}

如果您阅读 documentation,它说:

The SMTP protocol provider supports the following properties, which may be set in the JavaMail Session object. The properties are always set as strings; the Type column describes how the string is interpreted. For example, use

    props.put("mail.smtp.port", "888");

to set the mail.smtp.port property, which is of type int.

这些属性实际上应该是字符串,但较新版本的 JavaMail 也将它们作为整数接受,但不接受长整型。

另外请注意,您可以 get rid of your Authenticator 因为您将用户名和密码直接传递给连接方法。而且您不需要设置主机和端口的属性,因为您也直接传递它们。