简体   繁体   English

Tomcat中JNDI的Java Mail API配置文档

[英]Documentation of Java Mail API configuration for JNDI in Tomcat

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. 我花了几天时间弄清楚如何通过具有身份验证的 JNDI在Tomcat中配置javax.mail.Session,现在我可以了,但要深入研究代码。

In this time I have seen the worst code ever: javax.mail.Service#connect(String,String,String,String) Version 1.4.1 在这段时间里,我看到了有史以来最糟糕的代码: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();
    }

When is the password assigned? 何时分配密码? and why is it checked against null twice? 为什么要对null进行两次检查? – and then realize that the else does not belong to the if above. –然后意识到else不属于上述if。 (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.org"
    mail.smtp.from="test@example.com"       
    />

Pay attention to the fact that it is „password“ and „mail.smtp.user“ or „mail.user“ but not „mail.smtp.password“ or “user”. 请注意,它是“ password”和“ mail.smtp.user”或“ mail.user”,而不是“ mail.smtp.password”或“ user”。

At least the magic is done in Tomcats org.apache.naming.factory.MailSessionFactory . 至少魔术是在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. 如果属性password和属性mail.smtp.usermail.user退出,则该工厂将javax.mail.Authenticator添加到邮件会话。

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. 顺便说一句:我解释得更详细一些,以帮助其他人遇到同样的问题。

This is simply a bug in the documentation. 这只是文档中的错误。 Someone has already raised this on the Tomcat bug tracker 有人已经在Tomcat Bug Tracker上提出了这个问题

https://bz.apache.org/bugzilla/show_bug.cgi?id=53665 https://bz.apache.org/bugzilla/show_bug.cgi?id=53665

I suggest you register and vote for the bug. 我建议您注册该错误并对其投票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM