Spring mail发送邮件及BUG解决

正确Demo如下

public class testMail {  
    public static void sendEmail() throws Exception {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        Properties javaMailProperties = new Properties();
        javaMailProperties.setProperty("mail.smtp.auth", "true");
        javaMailProperties.setProperty("mail.smtp.ssl.trust", "mx.xxx.com");
        javaMailProperties.setProperty("mail.smtp.starttls.enable", "true");
         javaMailProperties.setProperty("mail.debug", "true");
        javaMailProperties.setProperty("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(javaMailProperties);
        senderImpl.setHost("mx.xxx.com");
        senderImpl.setPort(587);
        senderImpl.setUsername("zhutougg@xxx.com"); // 根据自己的情况,设置username
        senderImpl.setPassword("zhutougg"); // 根据自己的情况, 设置password
        senderImpl.setDefaultEncoding("UTF-8");

        // 建立邮件消息
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo("zhutougg@xxx.com");
        mailMessage.setFrom("zhutougg@xxx.com");
        mailMessage.setSubject(" 测试简单文本邮件发送! ");
        mailMessage.setText(" 测试我的简单邮件发送机制!! ");
        // 发送邮件
        senderImpl.send(mailMessage);
        System.out.println(" 邮件发送成功.. ");
    }

    public static void main(String[] args) throws Exception {
        sendEmail();
    }
}

碰到的BUG:

Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is 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. Failed messages: 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; message exception details (1) are:
Failed message 1:  
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:2140)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:738)
    at javax.mail.Service.connect(Service.java:366)
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:486)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:406)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:307)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)
    at com.zhutougg.mail.testMail.sendEmail(testMail.java:31)
    at com.zhutougg.mail.testMail.main(testMail.java:36)
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  
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:619)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:546)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2135)
    ... 8 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  
    at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
    at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
    at sun.security.validator.Validator.validate(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
    ... 19 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
    at java.security.cert.CertPathBuilder.build(Unknown Source)
    ... 25 more

这个BUG的问题就是不信任SSL的证书,可以通过导入证书方法解决,也可以通过忽略证书方法,即上述代码中的javaMailProperties.setProperty("mail.smtp.ssl.trust", "mx.xxx.com");

碰到的BUG二:

Exception in thread "main" org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first  
; message exception details (1) are:
Failed message 1:  
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2343)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1808)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1285)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:433)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:307)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)
    at com.zhutougg.mail.testMail.sendEmail(testMail.java:31)
    at com.zhutougg.mail.testMail.main(testMail.java:36)

加入starttls即可,javaMailProperties.setProperty("mail.smtp.starttls.enable", "true");

欢迎大家加入小密圈,一起交流进步

zhutougg

继续阅读此作者的更多文章