Search code examples
javaemailtomcatserver

javax.mail.MessagingException: Can't send command to SMTP host; SSLHandshakeException: No appropriate protocol


We have a Legacy Web Application which was built on Java 1.7, mail1.4.jar and deployed in tomcat 7. While triggering a mail with attachement to SMPT (nowdays everything move to Microsoft cloud), we are getting below error.

javax.mail.MessagingException: Can't send command to SMTP host; nested exception is: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1420) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408) at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:847) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:384) at javax.mail.Service.connect(Service.java:297)

Getting all details from property file and setting to properties object i.e., SMPT.HOST: office365 , SMPT.PORT : 587, SMPT.AUTH=true and so on. PFB details.

Code Snippet :

  final String username = ConfReader.getPropertyValue("mail.username");
  final String passwd = ConfReader.getPropertyValue("mail.password");
  from = ConfReader.getPropertyValue("mail.from");
  String port = ConfReader.getPropertyValue("mail.port"); 
  
  Properties props = new Properties();
  props.put("mail.smtp.host", ConfReader.getPropertyValue("mail.smtp.host"));
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", port);
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.debug", "true");

  Session session = Session.getInstance(props, new Authenticator() {
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {
          LogWriter.logErrorMessage("username :: "
                  + username + "passwd ::"+passwd);
          return new PasswordAuthentication(username, passwd);
      }
  });
  
    Message message = new MimeMessage(session);
  try
  {
    
     message.setFrom(new InternetAddress(from));

     final InternetAddress[] toAddress =
        InternetAddress.parse(toEmails);

     final InternetAddress[] ccAddress =
        InternetAddress.parse(ccEmails);
     
     message.setRecipients(Message.RecipientType.TO, toAddress);
     message.setRecipients(Message.RecipientType.CC, ccAddress);
     message.setSubject(subject);
    
     /*
      * Multipart to create the mail content.
      */
     final Multipart multipart = new MimeMultipart();
     /*
      * html body part to set in multipart
      */
    
     final MimeBodyPart htmlBodyPart = new MimeBodyPart();
     htmlBodyPart.setContent(htmlBody, "text/html");
     multipart.addBodyPart(htmlBodyPart);

     /*
      * source to read attachment content
      */
     DataSource source =
        new ByteArrayDataSource(bytes, "application/excel");
     /*
      * MimeBodyPart to hold the attachment content.
      */
     MimeBodyPart attachmentBodyPart = new MimeBodyPart();
     DataHandler handler = new DataHandler(source);
     attachmentBodyPart.setHeader("Content-Disposition",
        "attachment;filename=" + attachmentName);
     attachmentBodyPart.setDataHandler(handler);
     attachmentBodyPart.setFileName(attachmentName);
     multipart.addBodyPart(attachmentBodyPart);

     message.setContent(multipart);
     /*
      * following line to send email, if it fails, corresponding catch
      * block executes, which is logged to let know user if any error
      */
    
     Transport.send(message);
    
  }
  catch (AddressException e)
  {
     /*
      * email sending failed, logging the information
      */
    
     LogWriter
        .logErrorMessage("Address Exception : " + e.getMessage());
    
  }
  catch (MessagingException e)
  {
     /*
      * email sending failed, logging the information
      */
    
     LogWriter.logErrorMessage("Messaging Exception : "
        + e.getMessage());
    
 
  }

We have tried below options

  1. We have updated the Mail.14.7.jar

  2. added below properties in code props.put("mail.smtp.starttls.enable", "true"); props.put("mail.debug", "true");

  3. removed the "jdk.tls.disabledAlgorithms" security property in the java.security file installed jre folder of deployed server.

  4. Changed the port :25 and make "mail.smtp.starttls.enable"=true.

  5. added below properties in code props.put("mail.smtp.ssl.protocols", "TLSv1.2"); props.put("mail.smtps.ssl.protocols", "TLSv1.2");

But Still we are facing same issue.

Could you please help us in provide any other solution which will resolve this issue.

Thanks in advance.


Solution

  • I had a similar problem in the past. As others have said, this may be subtly dependent on all sorts of things.

    The changes which I made at the time, and which worked for me, were to set the mail.smtps.ssl.protocols property to TLSv1.2 (as you've already done), but I also had to modify the javax.mail library version:
    old version javax.mail:mail:1.4
    new version: com.sun.mail:javax.mail:1.6.2

    [Yes, theoretically that is sub-optimal because you're now using a specific implementation version, instead of the generic API. But, pragmatically, in circumstances like mine and yours that might be necessary.]

    I know that the javax.mail API is now deprecated and replaced with a newer library, but you would almost definitely need other source code changes if moving to the replacement jakarta.mail library.