title: javax.mail.MessagingException: Connection reset while trying to access gmail using java mail API
body: <p>I was trying to read through the mails in my gmail account using java mail API. This is the code:</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  import java.awt.*;
  import javax.mail.*;
  import javax.mail.search.FlagTerm; 
  import javax.mail.Flags.Flag;
  public class MailPharser {
  /**
   * @param args
   */
  public void mailRead()
  {
      Folder inbox; 
      // TODO Auto-generated method stub
       Properties props = System.getProperties();
       props.setProperty("mail.store.protocol", "imaps"); 
       try
       {
           Session session = Session.getDefaultInstance(props, null); 
  
           Store store = session.getStore("imaps");
  
           store.connect("imap.gmail.com","&lt;mailid@gmail.com&gt;", "&lt;password&gt;"); 
           inbox = store.getFolder("Inbox"); 
           System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());
  
  
       }
       catch (Exception ex) 
       { System.out.println("Error caught"); ex.printStackTrace(); }
  
  }
  
  public static void main(String[] args) {
      MailPharser mp = new MailPharser();
      mp.mailRead();
  }   
  
  }
  </code></pre>
  
  <p>While running, i am getting the below error:</p>
  
  <pre><code>javax.mail.MessagingException: Connection reset;
    nested exception is:
  java.net.SocketException: Connection reset
  at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
  at javax.mail.Service.connect(Service.java:295)
  at javax.mail.Service.connect(Service.java:176)
  at MailPharser.mailRead(MailPharser.java:26)
  at MailPharser.main(MailPharser.java:40)
  Caused by: java.net.SocketException: Connection reset
  at java.net.SocketInputStream.read(Unknown Source)
  at com.sun.net.ssl.internal.ssl.InputRecord.readFully(Unknown Source)
  at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
  at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:548)
  at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:352)
  at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
  at com.sun.mail.iap.Protocol.&lt;init&gt;(Protocol.java:113)
  at com.sun.mail.imap.protocol.IMAPProtocol.&lt;init&gt;(IMAPProtocol.java:111)
  at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:637)
  ... 4 more
  </code></pre>
  
  <p>I added trace and see that the connect call is failing. Am i doing anything wrong? any help would do. thanks in advance...</p>