我通过使用Sender获得了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");
}
}发布于 2020-10-29 09:43:11
您需要将SMTP证书添加到java密钥存储区:How to import a .cer certificate into a java keystore?
或禁用证书检查(不推荐):How to bypass ssl certificate checking in java
https://stackoverflow.com/questions/64587893
复制相似问题