So I'm trying to send an email from a deployed spring application on heroku. The emails get sent perfectly fine when running locally, but on heroku I'm getting this error:
javax.mail.MessagingException: Could not convert socket to TLS; 2021-04-30T14:46:49.318141+00:00 app[web.1]: nested exception is: 2021-04-30T14:46:49.318145+00:00 app[web.1]: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) 2021-04-30T14:46:49.318644+00:00 app[web.1]: at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907) 2021-04-30T14:46:49.318767+00:00 app[web.1]: at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666) 2021-04-30T14:46:49.318886+00:00 app[web.1]: at javax.mail.Service.connect(Service.java:317) 2021-04-30T14:46:49.318972+00:00 app[web.1]: at javax.mail.Service.connect(Service.java:176) 2021-04-30T14:46:49.319037+00:00 app[web.1]: at javax.mail.Service.connect(Service.java:125)
However, this error just magically appeared. It was working totally fine on heroku until yesterday. My guess is maybe heroku just flagged it cause it thought it was suspicious. Anyone know any fixes?
The following is my code:
public static void sendEmail(User user) throws MessagingException {
//System.out.println("Preparing to send email");
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
String myAccountEmail = "-redacted-";
String password = "-redacted-";
User recipient = user;
javax.mail.Session session = javax.mail.Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myAccountEmail, password);
}
});
Message message = prepareMessage(session, myAccountEmail, recipient);
Transport.send(message);
//System.out.println("Message sent successfully");
}
Not sure if it's a proper solution or if it suits your case, but I've spent the last few days working on this very same issue and the solution was to upgrade the java version heroku runs my app.
In system.properties file on your application's root folder:
java.runtime.version=13
Turns out the java 11 image they are using has some old SSL versions going on and (as far as I am concerned) theres is no way, we as developers, to disable it.
It solved my problem and I hope it solve yours. Not sure it is something Heroku is going to fix in the future for java 11 dynos.
Okay, to fix this enable TLSv1.2
in your mail properties like so:
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.protocols", "TLSv1.2");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With