[英]I got the SSLHandshakeException by using Spring Boot Mail Sender
我通過使用 Spring Boot Mail Sender API 得到了 SSLHandshakeException。 我已經指定了我在下面編寫的所有代碼片段和屬性文件。 我應該怎么做才能修復這個錯誤?
記錄錯誤
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");
}
}
您需要將 SMTP 證書添加到 java 密鑰庫: 如何將 .cer 證書導入 java 密鑰庫?
或禁用證書檢查(不推薦): 如何繞過 Java 中的 ssl 證書檢查
您可以將您的郵件服務提供商添加為受信任的:
spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com
這一行應該添加到 application.properties 文件中。 解決方案適用於 Java 11。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.