我花了几天的时间来弄清楚如何通过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? – 然后意识到其他不属于if if. (这是原始的缩进).回到主题.
至少我发现正确的资源定义是:
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中完成的.如果属性密码和属性mail.smtp.user或mail.user退出,则此工厂将javax.mail.Authenticator添加到邮件会话.
现在我的问题是所有这些东西的文档在哪里.特别是关于用户名和密码的配置?
顺便说一句:我解释得更详细一些,以帮助其他人如何解决同样的问题.