Send email using both TLS and SSL connection in java


Below is the complete configuration settings to send mail using java. It also shows the way to send mail using TLS and SSL connection.


Below is the configuration to be done in java mail configuration to send mail using port 25


                props.put("mail.transport.protocol", "smtp");
                props.put("mail.smtp.port", "25");
                props.put("mail.smtp.host", "YOUR HOST NAME");
                props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props,new Authenticator() {
                           protected PasswordAuthentication getPasswordAuthentication() {
                               return new PasswordAuthentication(username, password);
                           }
                       });

message.setSubject("SUBJECT");                    
  message.setFrom("YOUR FROM ADDRESS");          
         message.setContent(messageText, "text/html");
         message.setContent(multipart);
         Transport.send(message);





/**** JavaMail API method to send an email via Gmail SMTP server ***************/




                final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
                props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
                props.setProperty("mail.smtp.socketFactory.fallback", "false");
                props.setProperty("mail.smtp.port", 587);
                props.setProperty("mail.smtp.socketFactory.port", "465");
                props.put("mail.smtp.host",smtp.gmail.com);
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.auth", "true");


Session session = Session.getDefaultInstance(props,
                       new Authenticator() {
                           protected PasswordAuthentication getPasswordAuthentication() {
                               return new PasswordAuthentication(username, password);
                           }
                       });

message.setSubject("SUBJECT");                    
  message.setFrom("YOUR FROM ADDRESS");          
         message.setContent(messageText, "text/html");
         message.setContent(multipart);
         Transport.send(message);



/**** JavaMail API method to send an email via SMTp.office365.com ****/


The Microsoft office exchange impelments NTLM authentication. So you need to have the latest mail.jar in your application.

The latest release of JavaMail includes EXPERIMENTAL support for the Microsoft NTLM authentication mechanism used by Exchange.


http://www.oracle.com/technetwork/java/index-138643.html  (javamail 1.4.7)

Note: You may get the following error if the application doesn't import latest version of mail.jar which supports NTLM authentication

GENERAL ERRORS: class javax.mail.MessagingException: 530 5.7.1 Client was not authenticated

The below code works fine with the below relase of mail.jar


props.put("mail.store.protocol", "SMTP");
                props.setProperty("mail.smtp.port", 587);
                props.put("mail.smtp.host",smtp.office365.com);
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.auth", "true");
                props.put("mail.debug", "true");


Session session = Session.getDefaultInstance(props,
                       new Authenticator() {
                           protected PasswordAuthentication getPasswordAuthentication() {
                               return new PasswordAuthentication(username, password);
                           }
                       });

message.setSubject("SUBJECT");                    
  message.setFrom("YOUR FROM ADDRESS");          
         message.setContent(messageText, "text/html");
         message.setContent(multipart);
         Transport.send(message);


Note: Also please make sure the FROM mail address should be same as the username


No comments:

Post a Comment