0
votes

I am facing a strange problem. I am connecting the gmail server through IMAP. It is connecting fine at the first attempt fine. But when I connecting the store for the second time, I am getting exception. Please help. Below is the code which I have used.

String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        try {

            Properties properties = new Properties();
            properties.setProperty("mail.store.protocol", "imaps");
    //      properties.setProperty("mail.imaps.socketFactory.fallback", "false");


            // set this session up to use SSL for IMAP connections
            properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            properties.setProperty("mail.imaps.socketFactory.fallback", "false");

            // use the simap port for imap/ssl connections.
        //  properties.setProperty("mail.imap.socketFactory.port", "993");

            URLName url = new URLName("imap", _INCOMINGMAILSERVER, 993, "", _USERNAME, _PASSWORD);

            Session session = Session.getInstance(properties,null);
            session.setDebug(true);

            //Store store = session.getStore("imaps");
            Store store = new IMAPSSLStore(session, url);


            if (store.isConnected()) {
                Log.v("Connected", "TRUE");
            } else {
                Log.v("Connected", "FALSE");
                /*
                 * store.connect(_OUTGOINGMAILSERVER, _USERNAME, _PASSWORD); if
                 * (store.isConnected()) store.close();
                 */


                Log.v("INCOMING SERVER", _INCOMINGMAILSERVER);
                //store.connect(_INCOMINGMAILSERVER, _USERNAME, _PASSWORD);
                store.connect();
            }
            connected = true;

THE EXCEPTION

================== 05-15 01:31:50.765: W/System.err(19008): javax.mail.MessagingException: SSL handshake aborted: ssl=0x2a8e5238: I/O error during system call, Connection reset by peer; 05-15 01:31:50.765: W/System.err(19008): nested exception is: 05-15 01:31:50.765: W/System.err(19008): javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x2a8e5238: I/O error during system call, Connection reset by peer 05-15 01:31:50.765: W/System.err(19008): at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:571) 05-15 01:31:50.765: W/System.err(19008): at javax.mail.Service.connect(Service.java:288) 05-15 01:31:50.765: W/System.err(19008): at javax.mail.Service.connect(Service.java:169) 05-15 01:31:50.765: W/System.err(19008): at javax.mail.Service.connect(Service.java:118) 05-15 01:31:50.765: W/System.err(19008): at com.dts.powermailmanager.Mailmanager._connectToImap(Mailmanager.java:218) 05-15 01:31:50.765: W/System.err(19008): at com.dts.powermailmanager.Mailmanager.connect(Mailmanager.java:365) 05-15 01:31:50.765: W/System.err(19008): at com.dts.classes.PowermailAsync.doInBackground(PowermailAsync.java:157) 05-15 01:31:50.769: W/System.err(19008): at com.dts.classes.PowermailAsync.doInBackground(PowermailAsync.java:1) 05-15 01:31:50.769: W/System.err(19008): at android.os.AsyncTask$2.call(AsyncTask.java:287) 05-15 01:31:50.781: W/System.err(19008): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 05-15 01:31:50.781: W/System.err(19008): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 05-15 01:31:50.781: W/System.err(19008): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 05-15 01:31:50.781: W/System.err(19008): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 05-15 01:31:50.781: W/System.err(19008): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 05-15 01:31:50.781: W/System.err(19008): at java.lang.Thread.run(Thread.java:856) 05-15 01:31:50.781: W/System.err(19008): Caused by: javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x2a8e5238: I/O error during system call, Connection reset by peer 05-15 01:31:50.796: W/System.err(19008): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method) 05-15 01:31:50.796: W/System.err(19008): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395) 05-15 01:31:50.796: W/System.err(19008): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.(OpenSSLSocketImpl.java:647) 05-15 01:31:50.796: W/System.err(19008): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:618) 05-15 01:31:50.796: W/System.err(19008): at com.sun.mail.iap.Protocol.initStreams(Protocol.java:132) 05-15 01:31:50.796: W/System.err(19008): at com.sun.mail.iap.Protocol.(Protocol.java:111) 05-15 01:31:50.796: W/System.err(19008): at com.sun.mail.imap.protocol.IMAPProtocol.(IMAPProtocol.java:104) 05-15 01:31:50.800: W/System.err(19008): at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:538) 05-15 01:31:50.800: W/System.err(19008): ... 14 more

OK NOW I AM USING THIS:-

Properties properties = new Properties();
properties.setProperty("mail.store.protocol", "imaps");
Session session = Session.getInstance(properties,null);
session.setDebug(true);
Store store = session.getStore("imaps"); 
store.connect(_INCOMINGMAILSERVER, _USERNAME, _PASSWORD);

Also I close the store after getting message stuffs.

But still the same error while connecting for the second time. It seems the server is not responding or the port is somewhat busy while connecting second time. I can't figure it out.

2

2 Answers

0
votes

Clean up your code by getting rid of all that socket factory stuff, and follow the procedure here for connecting to Gmail. Be sure to use Session.getStore instead of instantiating the IMAPSSLStore yourself.

0
votes

I also got the same exception. I found that it was due to TLS 1.0 protocol was not supported by server.

I found that Android device, http connection fails to the server where TLS 1.0 is not supported. I searched every where about the bug, but did not find anything related to this problem. And the problem was solved, when the TLS 1.0 protocol support was added to the server.

You can check your server / hostname protocol support using https://www.ssllabs.com/ssltest