【问题标题】:Java mail without ssl - PKIX path building failed:没有 ssl 的 Java 邮件 - PKIX 路径构建失败:
【发布时间】:2012-11-15 23:01:41
【问题描述】:

我正在使用 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);

已通过使用上述详细信息远程登录到我的 smtpHost 验证了 smtp 凭据。但是,当我在 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 的服务器的正确参数是什么?

【问题讨论】:

    标签: java smtp jakarta-mail starttls


    【解决方案1】:

    This JavaMail FAQ entry 应该会有所帮助。

    尝试像这样使用 MailSSLSocketFactory:

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

    【讨论】:

    • 我确实试过那个。使用了常见问题解答中描述的 InstallCert,我得到了异常“javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?”。看起来我的服务器根本没有使用 SSL。
    • 用 InstallCert 异常编辑了我的问题。
    • InstallCert 的问题在于,它只有在您可以开始建立 SSL 连接时才有效。如果您的服务器仅支持转换为 SSL (TLS) 连接的纯文本连接,则 InstalCert 将无济于事。您可能想查看 MailSSLSocketFactory 类,它可以让您更轻松地处理无法验证的自签名证书。
    • 如何使用 MailSSLSocketFactory?
    • MailSSLSocketFactory javadocs 的哪一部分不清楚?
    【解决方案2】:

    为我工作:)

    Properties props = new Properties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", "smtp.companydomain.biz"); // 
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "true"); 
            props.put("mail.smtp.starttls.enable", "true");`enter code here`
            props.put("mail.smtp.port", "25");
            props.put("mail.smtp.socketFactory.port", "25");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "true");
    
            MailSSLSocketFactory sf = null;
            try {
                sf = new MailSSLSocketFactory();
            } catch (GeneralSecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.socketFactory", sf);
    
            Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
    
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("mail@companydomain.biz", "password");
                }
            });
    
            mailSession.setDebug(true); // Enable the debug mode
    
            Message msg = new MimeMessage( mailSession );
    
            //--[ Set the FROM, TO, DATE and SUBJECT fields
            try {
                msg.setFrom( new InternetAddress( "mail@companydomain.biz" ) );
            } catch (AddressException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                msg.setRecipients( Message.RecipientType.TO,InternetAddress.parse("myemailid@companydomain.biz") );
            } catch (AddressException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //msg.setSentDate(new Date());
            try {
                msg.setSubject( "Hello World!" );
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            //--[ Create the body of the mail
            try {
                msg.setText( "Hello from my first e-mail sent with JavaMail" );
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            //--[ Ask the Transport class to send our mail message
            try {
                Transport.send( msg );
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }
    

    【讨论】:

      【解决方案3】:

      我在使用 java 8 时遇到了这个问题。更新此属性后问题解决了

      props.put("mail.smtp.ssl.trust", "smtp.gmail.com")
      
      if used spring boot in application.property
      
      spring.mail.properties.mail.smtp.ssl.trust = smtp.gmail.com
      

      我认为这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-03
        • 1970-01-01
        • 2011-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-16
        • 2018-10-04
        相关资源
        最近更新 更多