title: JavaMail SocketException: Connection reset
body: <p>I'm trying to send emails through JavaMail API but I end up receiving the SocketException: Connection reset.</p>
  
  <pre><code>import java.util.Properties;
  import javax.mail.*;
  import javax.mail.internet.*;
  
  public class SendMailSSL {
   public static void main(String[] args) {
  
  Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.gmail.com");
      props.put("mail.smtp.socketFactory.port", "465");
      props.put("mail.smtp.socketFactory.class",
      "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.port", "465");
  
       Authenticator auth = new Authenticator() {
  
               @Override
               protected PasswordAuthentication getPasswordAuthentication() {
                   return new PasswordAuthentication("userName@gmail.com","gmailPassword");
               }
           };
  
      Session session = Session.getDefaultInstance(props,auth);
  
  
      try {
  
      Message message = new MimeMessage(session);
      Address sender  = new InternetAddress("any@...");
      message.setFrom(sender);
  
      String recipients = "email1@...,email2@...,email2@...";
      String[] toList = recipients.split(",");
      System.out.println(toList.length);
      Address[] addressTo = new InternetAddress[toList.length];
  
      for(int i = 0; i &lt; toList.length; i++){
      addressTo[i] = new InternetAddress(toList[i]);  
      }
  
      for( int i=0; i &lt; addressTo.length; i++) { // changed from a while loop
              message.addRecipient(Message.RecipientType.TO, addressTo[i]);
          }
  
  
      message.setSubject("Testing Subject 5");
      message.setText("Dear Message ," +
      "\n\n HELLO, please! \n https://192.168.192.120:8181/centralWeb");
      System.out.println("SENDING MAIL......... " + new Date().toString());
  
      message.setHeader("Content-type", "text/html; charset=UTF-8");
      Transport.send(message);
  
      System.out.println("Done  " + new Date().toString());
  
      } catch (MessagingException e) {
      throw new RuntimeException(e);
      }
  
   }
  }
  </code></pre>
  
  <p>NetBeans Output:</p>
  
  <pre><code>    1
  SENDING MAIL......... Sun Apr 28 01:30:18 IST 2013
  Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
    nested exception is:
      java.net.SocketException: Connection reset
      at sendmailssl.SendMailSSL.main(SendMailSSL.java:65)
  Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
    nested exception is:
      java.net.SocketException: Connection reset
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
      at javax.mail.Service.connect(Service.java:313)
      at javax.mail.Service.connect(Service.java:172)
      at javax.mail.Service.connect(Service.java:121)
      at javax.mail.Transport.send0(Transport.java:190)
      at javax.mail.Transport.send(Transport.java:120)
      at sendmailssl.SendMailSSL.main(SendMailSSL.java:60)
  Caused by: java.net.SocketException: Connection reset
      at java.net.SocketInputStream.read(SocketInputStream.java:189)
      at java.net.SocketInputStream.read(SocketInputStream.java:121)
      at sun.security.ssl.InputRecord.readFully(InputRecord.java:312)
      at sun.security.ssl.InputRecord.read(InputRecord.java:350)
      at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1328)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
      at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503)
      at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234)
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
      ... 7 more
  Java Result: 1
  BUILD SUCCESSFUL (total time: 2 seconds)
  </code></pre>
  
  <p>I've tried disabling IPv6 and turning off my firewall too, but the problem persists. I am using Windows 7 x64, if that matters.</p>
  
  <p>Thanks to anyone who can help me fix this issue.</p>