tags:

views:

654

answers:

1

I read on several sites that, when using the JavaMail API, to set the property mail.smtp.ssl.enable to true. I have some code as follows:

props.put("mail.smtp.host", "exchangemail1.example.com");
props.put("mail.from", "[email protected]");
props.put("mail.smtp.starttls.enable", "true");
// I tried this by itself and also together with ssl.enable)
props.put("mail.smtp.ssl.enable", "true");

Session session = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, "[email protected]");  
    // also tried @gmail.com
msg.setSubject("JavaMail ssl test");
msg.setSentDate(new Date());
msg.setText("Hello, world!\n");
props.put("mail.smtp.auth", "false");

Transport trnsport;
trnsport = session.getTransport("smtp");
trnsport.connect();
msg.saveChanges(); 
trnsport.sendMessage(msg, msg.getAllRecipients());
trnsport.close();

This sends email, but:

  1. when I do traffic capture, I see it is not encrypted
  2. When using debug (props.put("mail.debug", "true")), I see that "isSSL false"

(I also tried above adding in props.put("mail.smtp.auth","true") + user/password....)

Any ideas what I am doing wrong?

+1  A: 

I'd suggest using Apache commons-email. It has setters for the most used properties (including SSL / TLS) and is more friendlier to use and sits ontop of the JavaMail API.

Update: I was looking at commons-email code, and saw these lines:

properties.setProperty("mail.smtp.starttls.enable", this.tls);
properties.setProperty("mail.smtp.auth", "true");

so, give these properties a try as well.

Bozho