javax.mail.MessagingException: Connection reset while trying to access gmail using java mail API

Ranjit Kumar Alexander picture Ranjit Kumar Alexander · Apr 4, 2012 · Viewed 11.7k times · Source

I was trying to read through the mails in my gmail account using java mail API. This is the 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","<[email protected]>", "<password>"); 
         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();
}   

}

While running, i am getting the below error:

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.<init>(Protocol.java:113)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:111)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:637)
... 4 more

I added trace and see that the connect call is failing. Am i doing anything wrong? any help would do. thanks in advance...

Answer

Bill Shannon picture Bill Shannon · Apr 4, 2012

It's probably a network problem unrelated to JavaMail, e.g., a proxy or firewall between you and Gmail.

If you want to find out whether it's your code that's broken or the network that's broken, you can test using the code that comes with JavaMail. If the JavaMail code works, then you know there's something wrong with your code, and you can use the JavaMail code in the FAQ to improve your code.

If you discover that it's a network problem, the JavaMail FAQ also has tips for debugging it further.