1 Reply Latest reply: Jul 31, 2013 2:25 PM by bshannon RSS

    enabled SSL + TLS


      Hello all,


      I'm a beginner in JavaMail, I have several question, can I use following case:


      SMTP + TLS + Authentication

      SMTP + TLS + without Authentication

      SMTP + Authentication + without TLS




      SMTP + SSL + TLS + Authentication

      SMTP + SSL + TLS + without Authentication

      SMTP + SSL + Authentication + without TLS


      Because I have the following code, it's correctly work for send a mail with returned Transporter, but no with SMTP only, SMTP + SSL, SMTP + SSL + TLS.

      I have the following exception for example:

      javax.mail.MessagingException: Exception reading response;

      nested exception is:

      javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

      Can you give me use properties and means to correctly use SSL and TLS ?


      Great thank !


      Best regards



      public static Transport getConnectedTransportForSending(String smtpServer, String username, String password, SmtpServerType protocolSec) throws MessagingException {
        Session session = null;
        Boolean isWithAuth = (username != null && !username.equals("")) && (password != null && !password.equals(""));
        Properties props = new Properties();
           String prefixMailSmtp = "mail.smtp";
        if (SmtpServerType.SSL.equals(protocolSec)) {
        //prefixMailSmtp += "s";
        useSSL(props, prefixMailSmtp);
              // props.put("mail.transport.protocol", "smtps");
        props.put(prefixMailSmtp+".port", "587");
        } else if (SmtpServerType.SSLTLS.equals(protocolSec)) {
        //prefixMailSmtp += "s";
        useSSL(props, prefixMailSmtp);
        useTLS(props, prefixMailSmtp);
               //props.put("mail.transport.protocol", "smtps");
        props.put(prefixMailSmtp+".port", "587");
        } else if (SmtpServerType.TLS.equals(protocolSec)) {
        useTLS(props, prefixMailSmtp);
        //props.put("mail.transport.protocol", "smtp");
        props.put(prefixMailSmtp+".port", "25");
        } else {
        props.put(prefixMailSmtp+".port", "25");
        //props.put("mail.transport.protocol", "smtp");
        props.put(prefixMailSmtp+".socketFactory.fallback", "false");
        if (smtpServer != null) {
        props.put(prefixMailSmtp+".host", smtpServer);
        if (isWithAuth) {
        Authenticator auth = new ServerAuthenticator(username, password);
        props.put(prefixMailSmtp+".auth", "true");
        session = Session.getInstance(props, auth);
        } else {
        session = Session.getInstance(props, null);
        Transport transporter = session.getTransport("smtp");
        transporter.connect(smtpServer, username, password);
        return transporter;
        private static void useSSL(Properties props, String prefixMailSmtp){
        props.put(prefixMailSmtp+".socketFactory.port", "587");
        props.put(prefixMailSmtp+".socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.ssl.enable", "true");
        private static void useTLS(Properties props, String prefixMailSmtp){
        props.put(prefixMailSmtp+".starttls.enable", "true");
        public static class ServerAuthenticator extends Authenticator {
        private PasswordAuthentication authentication;
        public ServerAuthenticator(String username, String password) {
        authentication = new PasswordAuthentication(username, password);
        protected PasswordAuthentication getPasswordAuthentication() {
        return authentication;
        • 1. Re: enabled SSL + TLS

          You can simplify your code by getting rid of the socket factory stuff.


          If you connect using SSL to begin with, there's no need to use "TLS" (by which I assume you mean the STARTTLS command that switches a plain text connection to an SSL/TLS connection).


          And of course whether you're required to use SSL or required to use STARTTLS or required to authenticate depends entirely on the configuration of the mail server.