gpt4 book ai didi

JAVA Mail 和 Office 365 在连接时挂起

转载 作者:行者123 更新时间:2023-12-02 11:07:55 31 4
gpt4 key购买 nike

我们有一个应用程序,它严重依赖 JAVA Mail 来发送电子邮件。我们已按如下方式设置连接超时属性(从长值设置,是否需要整数才能生效?):

props.put("mail.smtp.timeout",           1000L);
props.put("mail.smtp.connectiontimeout", 1000L);

一段时间后,应用程序停止运行并且永远不会从 Office 365 smtp 帐户恢复(仅发生在 Office 365 中)。我们在 JAVA 邮件中启用了 Debug模式,失败的行如下:

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因为您将用户名和密码直接传递给 connect 方法。而且您不需要设置主机和端口的属性,因为您也直接传递它们。

关于JAVA Mail 和 Office 365 在连接时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50799045/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com