title: Java Mail over TLS
body: <p>I am trying to send an email from my program through a TLS connection. Here is my code</p>
  
  <pre><code>    final String username = "XXXXXX";
      final String password = "XXXXX"; 
      Properties props = new Properties();
      props.put("mail.transport.protocol", "smtp");
      props.put("mail.smtp.starttls.enable","true");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.host", "mail.xxxx.com");
      props.put("mail.smtp.port", "587");
  
      Session session = Session.getInstance(props,
              new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
              });
  
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("from@xxxx.com"));
      message.setRecipients(Message.RecipientType.TO,
          InternetAddress.parse(to_address));
      message.setSubject("Test Mail"); 
      message.setText("TestMail ");
      Transport.send(message)
  </code></pre>
  
  <p>My email gateway has incoming Mail settings with SSL enabled and outgoing with TLS enabled on port 587. I am able to configure this settings in outlook and it's working fine. But through my java program it's saying "Connection Refused".
  Help Appreciated!</p>
  
  <h1>Worked Finally:</h1>
  
  <p>I used the <a href="http://kenai.com/projects/javamail/pages/InstallCert">InstallCert</a> program to import the certicate to generate jssecacerts file and I added the file to my /jre/lib/security/ path. here is my working code</p>
  
  <pre><code>    properties.put("mail.transport.protocol", "smtp");
      properties.put("mail.smtp.host", "XXXXXX");  
      properties.put("mail.smtp.port", "465"); 
      properties.put("mail.smtp.ssl.enable", true);
      properties.put("mail.smtp.socketFactory.port", "465");
      properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
      properties.put("mail.smtp.socketFactory.fallback", "false"); 
      properties.put("mail.smtp.quitwait", "false"); 
      properties.put("mail.smtp.auth", "true"); 
  </code></pre>