我花了幾天的時間來弄清楚如何通過JNDI 在Tomcat中配置javax.mail.Session,身份驗證爲,現在我明白了,但只有在深入瞭解碼。Tomcat中JNDI的Java Mail API配置文檔
在這個時候,我已經看到了有史以來最糟糕的代碼:javax.mail.Service#連接(字符串,字符串,字符串,字符串)版本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檢查兩次? - 然後意識到其他不屬於如果以上。 (這是原始縮進)。返回主題。
至少我發現正確的資源定義是:
<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="[email protected]"
/>
注重事實,即它是「密碼」和「mail.smtp.user」或「mail.user」,而不是「mail.smtp.password」或「user」。
至少魔法是在雄貓org.apache.naming.factory.MailSessionFactory
完成。如果退出屬性password
和屬性mail.smtp.user
或mail.user
,此工廠將在郵件會話中添加javax.mail.Authenticator
。
現在我的問題是所有這些東西的文檔在哪裏。特別是關於用戶名和密碼的配置?
順便說一句:我解釋了一點更詳細,以幫助其他如何有同樣的問題。
2012年8月發佈的一個問題在https://issues.apache.org/bugzilla/show_bug.cgi?id=53665 – 2012-10-15 00:29:48
我想說最後的'else'應該被刪除。大多數情況下,這是由於長時間的代碼演變。 – EJP 2012-10-15 09:30:40