错误的主机发送 SMTP

Wrong host sending SMTP

我正在尝试使用 javax.mail 发送邮件。这是我的代码:

Properties props = new Properties();        
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.port", port);
props.setProperty("mail.user", user);
props.setProperty("mail.password", password);   

Session session = Session.getDefaultInstance(props);

但是我得到这个错误:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.wrong.server.com, port: 25;

有趣的是 "smtp.wrong.server.com" 不是我作为主机传递的值。

这就像 Session.getDefaultInstance(props) 正在返回一个已创建的会话,但主机名错误。

我的 EAR 中没有任何其他地方使用了 javax.mail(至少在我的代码中没有,也许在第三方依赖项中?)。

当然,这种行为只发生在 PRO 环境中。在 DEV 和 TEST 环境中部署的相同 EAR 工作正常。

任何帮助将不胜感激

这表明您没有为您的属性使用正确的密钥。请参阅 javax.mail.Session

的 Javadoc

It is expected that the client supplies values for the properties listed in Appendix A of the JavaMail spec (particularly mail.store.protocol, mail.transport.protocol, mail.host, mail.user, and mail.from) as the defaults are unlikely to work in all cases.

问题出在 Session.getDefaultInstance。我应该使用 Session.getInstance

来自 javadoc:

getDefaultInstance

(...)默认会话可能可用于在同一 Java 虚拟机中执行的所有代码(...)后续调用 return 由第一次调用,并忽略传递的 Properties 对象。使用getInstance方法,每次调用该方法都会得到一个新的Session对象。