我花了几天时间弄清楚如何通过JNDI 带有身份验证在Tomcat中配置javax.mail.Session. >
这一次,我看到了有史以来最糟糕的代码:javax.mail.Service#connect(String,String,String,String)版本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" mail.smtp.from="test@example" />请注意,它是"password"和"mail.smtp.user"或"mail.user",而不是"mail.smtp.password"或"user".
至少魔术是在Tomcats org.apache.naming.factory.MailSessionFactory中完成的.如果属性password和属性mail.smtp.user或mail.user退出,则工厂将javax.mail.Authenticator添加到邮件会话中.
现在我的问题是所有这些东西的文档在哪里.尤其是关于用户名和密码的配置?
顺便说一句:我对它进行了详细解释,以帮助其他人遇到同样的问题.
解决方案Tomcat文档是受限制的,但是源代码在这里: javasourcecode/html/open-source/tomcat/tomcat-7.0.19/org/apache/naming/factory/MailSessionFactory.java.html
I spend several days to figure out how to configure an javax.mail.Session in Tomcat via JNDI with authentication, now I get it but only after a deep dive in the code.
In this time I have seen the worst code ever: 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(); }When is the password assigned? and why is it checked against null twice? – and then realize that the else does not belong to the if above. (This is the original indentation). Back to topic.
At least I found that the correct resource definition is:
<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" mail.smtp.from="test@example" />Pay attention to the fact that it is „password" and „mail.smtp.user" or „mail.user" but not „mail.smtp.password" or "user".
At least the magic is done in Tomcats org.apache.naming.factory.MailSessionFactory. This factory add an javax.mail.Authenticator to the mail session if an property password and an property mail.smtp.user or mail.user exits.
Now my question is where is the documentation for all that stuff. Especially about configuration of username and password?
Btw: I explained it a bit more detailed to help other how has the same problem.
解决方案Tomcat documentation is limited but the source code is here: javasourcecode/html/open-source/tomcat/tomcat-7.0.19/org/apache/naming/factory/MailSessionFactory.java.html
更多推荐
Tomcat中JNDI的Java Mail API配置文档
发布评论