Javamail: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException 从 VPS 发送邮件时

Posted

技术标签:

【中文标题】Javamail: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException 从 VPS 发送邮件时【英文标题】:Javamail: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException when sending mail from VPS 【发布时间】:2011-12-17 21:44:25 【问题描述】:

我有以下问题。我有一个 Seam Web 应用程序,它具有电子邮件撰写和使用 Javamail 发送的功能。该应用程序在我的本地 JBoss 应用程序服务器中发送邮件很好,但它在我的 myhosting.com VPS 中安装的 JBoss 服务器上不起作用。我读到这个问题可能是因为我尝试连接的 Web 服务器或 URL 没有来自授权 CA 的有效证书,但我运行了一个实用程序 (http://cold-caffein.blogspot.com/2011/07/looks-like-article-no-more-unable-to.html) 来验证证书和他们似乎很好(我正在连接到 Gmail)。

设置Session属性的相关代码如下:

props.put("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable", "true");

我读到该问题的另一个原因可能是使用将 mail.smtp.starttls.enable 设置为 true,但如果我省略第二行,则代码根本不起作用(在我的本地服务器或 VPS 中)或将属性设置为 false。

我不知道问题是否与 myhosting 提供的 mail2web 邮件服务有关(请查看我在下面提供的两个日志中的第 9 行,这就是事情开始不同的地方)。以下是本地 Javamail 日志和 VPS 的服务器日志(后者显示了我遇到的错误):

本地日志:

18:57:31,129 INFO  [STDOUT] DEBUG: setDebug: JavaMail version 1.4ea
18:57:31,227 INFO  [STDOUT] DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
18:57:31,383 INFO  [STDOUT] DEBUG SMTP: useEhlo true, useAuth true
18:57:31,384 INFO  [STDOUT] DEBUG SMTP: useEhlo true, useAuth true
18:57:31,384 INFO  [STDOUT] DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false
18:57:31,677 INFO  [STDOUT] 220 mx.google.com ESMTP c8sm4881699yhm.14
18:57:31,677 INFO  [STDOUT] DEBUG SMTP: connected to host "smtp.gmail.com", port: 25
18:57:31,680 INFO  [STDOUT] EHLO SOFMANSERVER
18:57:32,088 INFO  [STDOUT] 250-mx.google.com at your service, [200.25.201.121]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250 PIPELINING
18:57:32,088 INFO  [STDOUT] DEBUG SMTP: Found extension "SIZE", arg "35882577"
18:57:32,088 INFO  [STDOUT] DEBUG SMTP: Found extension "8BITMIME", arg ""
18:57:32,088 INFO  [STDOUT] DEBUG SMTP: Found extension "STARTTLS", arg ""
18:57:32,088 INFO  [STDOUT] DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
18:57:32,088 INFO  [STDOUT] DEBUG SMTP: Found extension "PIPELINING", arg ""
18:57:32,088 INFO  [STDOUT] STARTTLS
18:57:32,212 INFO  [STDOUT] 220 2.0.0 Ready to start TLS
18:57:32,803 INFO  [STDOUT] EHLO SOFMANSERVER
18:57:33,829 INFO  [STDOUT] 250-mx.google.com at your service, [200.25.201.121]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250-ENHANCEDSTATUSCODES
250 PIPELINING
18:57:33,829 INFO  [STDOUT] DEBUG SMTP: Found extension "SIZE", arg "35882577"
18:57:33,829 INFO  [STDOUT] DEBUG SMTP: Found extension "8BITMIME", arg ""
18:57:33,829 INFO  [STDOUT] DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH"
18:57:33,829 INFO  [STDOUT] DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
18:57:33,829 INFO  [STDOUT] DEBUG SMTP: Found extension "PIPELINING", arg ""
18:57:33,829 INFO  [STDOUT] DEBUG SMTP: Attempt to authenticate
18:57:33,829 INFO  [STDOUT] AUTH LOGIN
...

邮件发送成功。

VPS 日志:

2011-10-26 19:54:10,290 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG: setDebug: JavaMail version 1.4ea
2011-10-26 19:54:10,292 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
2011-10-26 19:54:10,335 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG SMTP: useEhlo true, useAuth true
2011-10-26 19:54:10,335 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG SMTP: useEhlo true, useAuth true
2011-10-26 19:54:10,335 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false
2011-10-26 19:54:10,418 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) 220 xsmtp05.mail2web.com ESMTP Exim Wed, 26 Oct 2011 19:54:09 -0400
2011-10-26 19:54:10,418 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG SMTP: connected to host "smtp.gmail.com", port: 25
2011-10-26 19:54:10,418 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) EHLO <my-vps-host-name>
2011-10-26 19:54:10,456 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) 250-xsmtp05.mail2web.com Hello <my-vps-host-name> [<my-vps-ip>]
250-SIZE 104857600
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
2011-10-26 19:54:10,456 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG SMTP: Found extension "SIZE", arg "104857600"
2011-10-26 19:54:10,456 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG SMTP: Found extension "PIPELINING", arg ""
2011-10-26 19:54:10,456 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
2011-10-26 19:54:10,456 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG SMTP: Found extension "STARTTLS", arg ""
2011-10-26 19:54:10,456 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) DEBUG SMTP: Found extension "HELP", arg ""
2011-10-26 19:54:10,456 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) STARTTLS
2011-10-26 19:54:10,582 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) 220 TLS go ahead
2011-10-26 19:54:10,582 INFO  [STDOUT] (http-<my-vps-ip-and-jboss-port>-15) EHLO <my-vps-host-name>
2011-10-26 19:54:10,623 ERROR [<Class that sends mail>] (http-<my-vps-ip-and-jboss-port>-15) Can't send command to SMTP host
javax.mail.MessagingException: Can't send command to SMTP host;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    ...

    Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    ...

    Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    ...

    Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

我应该怎么做才能摆脱这个错误?

【问题讨论】:

您是否从 VPS 本身运行了上述实用程序?它的信任存储似乎有问题。 【参考方案1】:

问题已解决,既不是网络问题,也不是代码问题。我们使用的是 javamail-1.4,升级到 1.4.4 后,问题就消失了。

【讨论】:

【参考方案2】:

您可以在https://java.net/projects/javamail/pages/Home 升级库 javax.mail.jar(现在版本是 1.5.5)并添加代码:

MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true); 
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.ssl.socketFactory", sf);

【讨论】:

是的,更新库是解决方案。通过使用 Apache Commons Email,我们已经将所有这些“手动放置属性”抛在了脑后。 比java邮件好用吗? 它建立在 JavaMail 之上:“Commons Email 旨在提供用于发送电子邮件的 API。它建立在 Java Mail API 之上,旨在简化。” (commons.apache.org/proper/commons-email)。它允许您以更少的 LOC 和更少的麻烦实际发送邮件。

以上是关于Javamail: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException 从 VPS 发送邮件时的主要内容,如果未能解决你的问题,请参考以下文章

在 Android Studio 中调试时出现证书验证路径错误

JavaMail:利用JavaMail发送复杂邮件

JavaMail 读取消息

JavaMail| JavaMail配置属性

JavaMail发送简单邮件

在android开发中如何使用JavaMail程序