【问题标题】:I got the SSLHandshakeException by using Spring Boot Mail Sender我使用 Spring Boot Mail Sender 得到了 SSLHandshakeException
【发布时间】:2020-10-29 09:03:21
【问题描述】:

我使用 Spring Boot Mail Sender API 得到了 SSLHandshakeException。 我已经指定了我在下面编写的所有代码 sn-ps 和属性文件。我应该怎么做才能修复这个错误?

日志错误

Mail sending is started
Error occured: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: 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: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Process finished with exit code 0

应用程序属性

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username= myemail
spring.mail.password= mypass
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

服务层

@Service
public class SendEmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    public void sendEmail(String to, String body, String topic){
        try {
            System.out.println("Mail sending is started");
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom("mail@mail.com");
            simpleMailMessage.setTo(to);
            simpleMailMessage.setSubject(topic);
            simpleMailMessage.setText(body);
            javaMailSender.send(simpleMailMessage);
            System.out.println("Mail sending is completed");
        }catch (Exception e){
            System.out.println("Error occured: "+e.getMessage());
        }
    }
}

SpringBootApplication 类

@SpringBootApplication
public class MailSenderApplication {

    @Autowired
    SendEmailService sendEmailService;

    public static void main(String[] args) {
        SpringApplication.run(MailSenderApplication.class, args);
    }

    @EventListener(ApplicationReadyEvent.class)
    public void triggerWhenStarts(){
        sendEmailService.sendEmail("mutlueren01@gmail.com","This e-mail has sending by Spring Boot","Spring Boot Mail Sender TEST");
    }
}

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    您需要将 SMTP 证书添加到 java 密钥库:How to import a .cer certificate into a java keystore?

    或禁用证书检查(不推荐):How to bypass ssl certificate checking in java

    【讨论】:

      【解决方案2】:

      您可以将您的邮件服务提供商添加为受信任的:

      spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com

      此行应添加到 application.properties 文件中。解决方案适用于 Java 11。

      【讨论】:

        猜你喜欢
        • 2021-03-26
        • 2016-03-26
        • 2021-03-27
        • 1970-01-01
        • 1970-01-01
        • 2016-07-04
        • 2021-04-20
        • 2019-07-06
        • 2014-06-10
        相关资源
        最近更新 更多