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");
}