geronimo javamail与sun javamail冲突导致发送邮件失败

问题:
生产线上有一个需要发生邮件通知的服务,通过Exchange Hub Transport Server发送。但是发送邮件时收到了如下异常:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
at javax.mail.Transport.send(Transport.java:95)
at javax.mail.Transport.send(Transport.java:48)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
at org.apache.commons.mail.Email.send(Email.java:1267)



经过反复确认,发送邮件的各项配置:host,username,password,sendFrom,sendTo,Cc等均正确,无奈写了一段最精简的测试代码放到服务器上测试了一下,竟然成功。
这样就排除了Hub服务器配置的问题,于是打开了javamail的debug看一下输出:

发送邮件成功的debug信息中如下:
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true

而发送邮件失败的debug信息:
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@59a04a1b;.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc
DEBUG SMTP: useEhlo true, useAuth false

接下来的debug信息也确实反映出,发送邮件失败的程序在给服务器发送EHLO信号后,没有执行AUTH LOGIN就直接发送MAIL FROM,导致出现了
530 5.7.1 Client was not authenticated
信息。

不过两者依赖的都是javamail-1.4.4,而且都是通过Apache Commons Email组件来进行发送邮件操作,为何会有如此差异呢?

通过仔细观察生产线上出问题程序的依赖关系,发现该项目里还有一份geronimo-javamail_1.4_spec-1.6.jar,是cxf引入的依赖。
删除掉geronimo-javamail_1.4_spec-1.6.jar与geronimo-activation_1.1_spec-1.0.2.jar之后,发信功能正常。由此得出结论:

geronimo-javamail与sun javamail存在冲突。

让我感到不解的是:
Apache Commons Email组件在设置了用户名密码后,会将”mail.smtp.auth”设置为true,但是引入geronimo-javamail_1.4_spec-1.6.jar会略过AUTH LOGIN。

等看完geronimo-javamail源码后再分解。

加载评论框需要翻墙