acco
acco

Reputation: 33

javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) sending mail

Good morning,

I'm trying to send a mail in a java application running on Ubuntu with jakarta.mail-api version 2.1.2 and openjdk version 11.0.20.1

but I obtain following exception:

javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)

Here's my code:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.office365.com"); //SMTP Host
props.put("mail.smtp.port", "587"); //TLS Port
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.debug", "true");

Authenticator auth = new Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication("[email protected]", "yyyy");
   }
};

Session session = Session.getInstance(props, auth);

try {
   MimeMessage msg = new MimeMessage(session);
   msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
   msg.addHeader("format", "flowed");
   msg.addHeader("Content-Transfer-Encoding", "8bit");

   msg.setFrom(new InternetAddress("[email protected]"));
   msg.setReplyTo(InternetAddress.parse("[email protected]", false));
   msg.setSubject("Mail Subject");
   msg.setText("body", "UTF-8");
   msg.setRecipients(Message.RecipientType.TO,   InternetAddress.parse("[email protected]", false));
     
   Transport.send(msg);  
    
   log.info("Email Sent Successfully!!");
} catch (Exception e) {
   log.error("Error",e);
}

Same code works well on my Windows 11 machine with java version 11.0.19 installed. Any suggestion?

UPDATE: As suggested by dave_thompson_085 I've changed

props.put("mail.smtp.ssl.protocols", "TLSv1.2");

in

props.put("mail.smtp.ssl.protocols", "TLSv1.3");

but exception remains the same.

Checking java.security I've found:

jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, 
MD5withRSA, \
DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
include jdk.disabled.namedCurves

after commenting these three lines Exception become:

javax.mail.AuthenticationFailedException: null
at javax.mail.Service.connect(Service.java:306) ~[mail-1.4.jar:1.4]

after removing only TLSv1, TLSv1.1, I catch always:

javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)

Upvotes: 1

Views: 521

Answers (1)

jmehrens
jmehrens

Reputation: 11045

Your exception has mail-1.4.jar as the artifact that is being used. JavaMail 1.4 was released on April 25, 2006.

Update your dependencies so mail-1.4.jar is not being directly used or transitively used.

Upgrade to at JavaMail 1.6.4 or newer.

Upvotes: 1

Related Questions