spring boot mail发送邮件到自建服务器

spring boot mail 发送邮件到自建服务器(采坑指南)

  1. 引入依赖

            
            
                org.springframework.boot
                spring-boot-starter-mail
            
    
  1. 配置服务器地址

    spring:
      mail:
        host: mail.kcjz.com
        username: [email protected]
        password: kcjz
        port: 25
        properties:
    #      指定starttls加密
          mail:
            smtp:
              starttls:
                enable: true
    #            不认证ssl证书
              ssl:
                trust: mail.kcjz.com
    #            设置超时时间,默认是没有超时时间的
              connectiontimeout: 5000
              timeout: 3000
              writetimeout: 5000
    
    1. 需要指定加密方式是starttls,解决一下异常:

      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:2374)
        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:465)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:323)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:312)
      
    2. 对指定域名的ssl证书进行信任(ssl证书是自签发的服务器),解决以下异常:

      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:734)
        at javax.mail.Service.connect(Service.java:342)
        at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518)
        at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:323)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:312)
      
    3. 设置超时时间,默认是没有超时时间的

  2. 测试代码

        @Test
        void mail(){
            //建立邮件消息
            SimpleMailMessage mainMessage = new SimpleMailMessage();
            //发送方
            mainMessage.setFrom("[email protected]");
            //接收方
            mainMessage.setTo("*********@qq.com");
            //发送的标题
            mainMessage.setSubject("测试邮件");
            //发送的内容
            mainMessage.setText("我只是个测试邮件");
            javaMailSender.send(mainMessage);
        }
    

你可能感兴趣的:(spring boot mail发送邮件到自建服务器)