I am getting an SSLHandshakeException when trying to send an email from my SpringBoot app, running locally to a Gmail account. If I turn off my antivirus (Avast free edition), the issue doesn't exist and the email is being sent without any issues.
I was trying to find a solution by configuring my antivirus to allow the connections on port 465 but I am failing.
I couldn't find any other solution except turning off the antivirus which is not an acceptable solution for me.
Error exception message:
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 connect to SMTP host: smtp.gmail.com, port: 465;
Used dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Code executing email sending:
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
(...)
@Autowired
private JavaMailSender javaMailSender;
(...)
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom("XXXX@gmail.com");
mailMessage.setTo(receiver);
mailMessage.setSubject(subject);
mailMessage.setText(message);
try {
javaMailSender.send(mailMessage);
} catch (Exception e) {
throw new SendFailedException("Couldn't send an email: "+ e.getMessage());
}
Spring app properties:
spring.mail.host=smtp.gmail.com
spring.mail.port=465
spring.mail.username=XXXX@gmail.com
spring.mail.password=XXXX
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.enable = true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000