Personal tools

Oct 28, 2011

Using JavaMail with TLS

Snippets about sending mail though Java with TLS support

Here is an example to show you how to use JavaMail API method to send an email, using both TLS and SSL connection.

If you are using TLS, remember to disable this property:

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

 

and enable
props.put("mail.smtp.starttls.enable","true")

 

These are the complete properties to set: 
props.put("mail.smtp.host", server);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.debug","false");
props.put("mail.smtp.port",port);
props.put("mail.smtp.ssl.enable","true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.fallback", "false");

 

Remember that, when you set Authentication to server with PasswordAuthentication class, you could experience this error:

"javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted."

Send by server if user or password used for authentication failed. Usually means that server requires full user name (user@server.com) to be used.

Filed under:
comments powered by Disqus