我花了几天时间弄清楚如何通过 JNDI 使用身份验证 在 Tomcat 中配置 javax.mail.Session,现在我明白了,但只有在深入研究代码之后。
这段时间我看到了有史以来最糟糕的代码:javax.mail.Service#connect(String,String,String,String) Version 1.4.1
if (user == null) {
user = url.getUsername();
if (password == null) // get password too if we need it
password = url.getPassword();
} else {
if (password == null && user.equals(url.getUsername()))
// only get the password if it matches the username
password = url.getPassword();
}
密码是什么时候分配的?以及为什么要针对 null 进行两次检查? – 然后意识到 else 不属于上面的 if。 (这是原始缩进)。回到主题。
至少我发现正确的资源定义是:
<Resource name="email/session"
type="javax.mail.Session"
auth="Container"
password="secret"
mail.debug="false"
mail.transport.protocol="smtp"
mail.smtp.auth="true"
mail.smtp.user="testi"
mail.smtp.host="smtp.xxx.org"
mail.smtp.from="test@example.com"
/>
注意它是“password”和“mail.smtp.user”或“mail.user”,而不是“mail.smtp.password”或“user”。
至少在 Tomcats org.apache.naming.factory.MailSessionFactory
中完成了魔法。如果属性 password
和属性 mail.smtp.user
或 ,则此工厂将
退出。javax.mail.Authenticator
添加到邮件 session mail.user
现在我的问题是所有这些东西的文档在哪里。特别是关于用户名和密码的配置?
顺便说一句:我解释得更详细一些,以帮助其他有相同问题的人。
最佳答案
这只是文档中的一个错误。有人已经在 Tomcat bug tracker 上提出了这个问题
https://bz.apache.org/bugzilla/show_bug.cgi?id=53665
我建议您注册并为错误投票。
关于java - Tomcat 中 JNDI 的 Java Mail API 配置文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6372171/