I am trying to use JavaMailSender to send mails in spring boot, but I am getting this error:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate). Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
After doing some reading, I found it is because we are using TLSv1 or TLSv1.1 which is outdated, and we should use v1.2 or higher. I tried to add ssl.protocols property in application.yml with value TLSv1.2, but it does not seem to work. Here is my application.yml:
spring:
mail:
host: smtp.gmail.com
port: 587
username: ******@gmail.com
password: *******
protocol: smtp
tls: true
properties.mail.smtp:
auth: true
ssl.trust: smtp.gmail.com
starttls.required: true
starttls.enabled: true
ssl.protocols: TLSv1.2
and here is the method that sends the mail:
public void sendEmail(String to, String subject, String body) {
logger.info("Sending mail to : " + to);
SimpleMailMessage mail = new SimpleMailMessage();
mail.setTo(to);
mail.setSubject(subject);
mail.setText(body);
try {
javaMailSender.send(mail);
logger.info("Mail sent to " + to);
} catch (Exception e) {
logger.error("Could not send mail to " + to + ". Exception : " + e.getMessage());
}
}
I updated JavaMailSender version but it didn't fix the issue. The only thing that worked was removing TLSv1 and TLSv1.1 from jdk.tls.disabledAlgorithms as suggested in this answer, but it is only a temporary fix. How can make the mail sender use TLSv1.2 or higher? Is there anything wrong in the way I am defining the ssl.protocols property in application.yml?
This is the java version I am using:
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
and the spring boot starter mail version:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.5.3</version>
</dependency>