没有 ssl 的 Java 邮件 - PKIX 路径构建失败 :

标签 java smtp jakarta-mail starttls

我正在使用 java 邮件通过 smtp 发送电子邮件。下面给出的 smtp 设置:

        Properties props = new Properties();
        Object put = props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.user", smtpUser);
        props.put("mail.smtp.auth", true);
        props.put("mail.debug", mailDebug);
        props.put("mail.smtp.port", port);

smtp 凭据已通过使用上述详细信息远程登录到我的 smtpHost 进行了验证。但是,当我在 java 邮件中使用上述设置时,出现以下异常。

        250-AUTH PLAIN LOGIN
        250-STARTTLS
        250 HELP
        DEBUG SMTP: Found extension "SIZE", arg "52428800"
        DEBUG SMTP: Found extension "8BITMIME", arg ""
        DEBUG SMTP: Found extension "PIPELINING", arg ""
        DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
        DEBUG SMTP: Found extension "STARTTLS", arg ""
        DEBUG SMTP: Found extension "HELP", arg ""
        DEBUG SMTP: Attempt to authenticate
        DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
        DEBUG SMTP: AUTH LOGIN command trace suppressed
        DEBUG SMTP: AUTH LOGIN failed
        Nov 29, 2012 11:54:40 AM com.Test main
        SEVERE: null
        javax.mail.AuthenticationFailedException: 535 Incorrect authentication data

当我添加行时:

        props.put("mail.smtp.starttls.enable", false);

它再次生成相同的身份验证失败异常。

如果我将 ma​​il.smtp.starttls.enable 设置为 true,身份验证成功,但出现以下异常:

     220 TLS go ahead
     Nov 28, 2012 5:32:36 PM com.Test main
     SEVERE: null
     javax.mail.MessagingException: Could not convert socket to TLS;
     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
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1918)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:652)
    at javax.mail.Service.connect(Service.java:317)

在浏览了关于第二个异常的各种论坛帖子后,我运行了 InstallCert 程序来获取服务器的自签名证书。 InstallCert 引发以下异常:

            Opening connection to mydomain.com.au:443...
            Starting SSL handshake...
            javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
                    at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:542)
                    at sun.security.ssl.InputRecord.read(InputRecord.java:374)
                    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:850)
                    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1190)
                    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1217)
                    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1201)
                    at InstallCert.main(InstallCert.java:100)
            Could not obtain server certificate chain

所以,看起来我的服务器没有 ssl,但启用了 starttls。将开启 STARTTLS 的邮件发送到没有 ssl 的服务器的正确参数是什么?

最佳答案

This JavaMail FAQ entry应该有帮助。

尝试像这样使用 MailSSLSocketFactory:

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

关于没有 ssl 的 Java 邮件 - PKIX 路径构建失败 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13599234/

相关文章:

JavaMail API 不适用于我,但有异常 com.sun.mail.util.MailConnectException : Couldn't connect to host, 端口

java - 使用smtp java读取发送的邮件

java - 我应该导入 android.graphics.camera 还是 android.hardware.camera?

java - 从 ArrayList 中删除特定字符串

java - 许多 Tomcat 线程正在 hibernate - org.apache.catalina.connector.CoyoteAdapter.postParseRequest

java - java破折号参数(如-D)和没有破折号的有什么区别?

smtp - 需要在 IIS 上为 ASP.Net Core 身份服务器配置 SMTP 设置以进行电子邮件验证

android - 利用 Google 帐户凭据在 Android 中发送电子邮件

wordpress - 如何在 Docker 容器中从 WordPress(联系表格)发送电子邮件?

java - 可用的密码网络应用程序