This discussion is archived
1 Reply Latest reply: Jul 31, 2013 12:25 PM by bshannon RSS

enabled SSL + TLS

adryen31300 Newbie
Currently Being Moderated

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


 

and


 

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

 

Adryen


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);
  }
  @Override
  protected PasswordAuthentication getPasswordAuthentication() {
  return authentication;
  }
  }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points