This discussion is archived
4 Replies Latest reply: Jan 23, 2013 12:13 AM by 984643 RSS

java mail Store.connect hangs - doesn't timeout

984643 Newbie
Currently Being Moderated
I am using Java mail api to programming on receiveing mail of various of mail box(pop3/Imap) by multithread.
for each mailbox, before calling store.getFolder("INBOX"), I called the follow function to connect to Imap server

public void initStore(){
          synchronized (syncObj)
          {
               if(store == null || !store.isConnected()){
                    
                    Properties prop = new Properties();
                    
                    prop.setProperty("mail.imap.host", imapserver);
                    
                    prop.setProperty("mail.imap.auth.plain.disable", "true");
               
          if(isSsl){//if user set isSsl to “true” when config the mailbox
               
               String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
prop.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
prop.setProperty("mail.imap.socketFactory.fallback", "false");
prop.setProperty("mail.imap.port", "993");
prop.setProperty("mail.imap.socketFactory.port", "993");

prop.setProperty("mail.imap.partialfetch", "false");
prop.setProperty("mail.mime.multipart.ignoreexistingboundaryparameter", "true");
          
          }

               try
               {
                         
                    Session mailsession=Session.getInstance(prop,null);
                         
               session = mailsession;
               mailsession.setDebug(true);
                         
          store = (IMAPStore)mailsession.getStore("imap");
                    store.connect(imapserver,user,pwd);
               }
               catch (Exception e)
               {
                    e.printStackTrace();
               }
          }
}

usually,it works normal,but somtimes,it hangs at "store.connect(imapserver,user,pwd);"

Debug Info:
DEBUG: setDebug: JavaMail version 1.4.5

DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
DEBUG: mail.imap.partialfetch: false
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: disable AUTH=PLAIN
DEBUG: trying to connect to host "imap.gmail.com", port 993, isSSL false
* OK Gimap ready for requests from 219.149.12.125 m26if10520967yhn.37
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2
A0 OK Thats all she wrote! m26if10520967yhn.37
DEBUG IMAP: AUTH: XOAUTH
DEBUG IMAP: AUTH: XOAUTH2
DEBUG: protocolConnect login, host=imap.gmail.com, user=tracyamazon087@gmail.com, password=<non-null>
DEBUG IMAP: LOGIN command trace suppressed//after here, there are nothing to output by debug


How should I do to resolve these? Waiting For your answer,Thanks!
  • 1. Re: java mail Store.connect hangs - doesn't timeout
    bshannon Pro
    Currently Being Moderated
    You probably don't need any of the socketFactory properties, see this:
    http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes

    Your problem is that you're running into a network service that isn't responding for whatever reason.
    You need to set a timeout. The javadocs for the com.sun.mail.imap package describes all the
    properties you can set. Since you're actually making the socket connection successfully, a
    connection timeout won't help, but a read/write timeout might. If you're hanging inside of the SASL
    library that's doing XOAUTH authentication, you might need a different timeout. Worst case,
    you might need to implement an application level timeout that does the Store.connect in a
    different thread and kills that thread if it doesn't return quickly enough.
  • 2. Re: java mail Store.connect hangs - doesn't timeout
    984643 Newbie
    Currently Being Moderated
    Thanks bshannon!
    according to the log added by myself, i think the program is hanging inside the following code:
    store.connect(imapserver,user,pwd);
    so that, following three properties of imap timeout ,which should i set?
    1--mail.imap.connectionpooltimeout
    2--mail.imap.connectiontimeout
    3--mail.imap.timeout
    and if i set the timeout property, what kind if exception should to be catched when timeout coming?

    by the way, i also find another exception in the debug info as follow:
    DEBUG: setDebug: JavaMail version 1.4.5

    DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
    DEBUG: mail.imap.fetchsize: 16384
    DEBUG: mail.imap.statuscachetimeout: 1000
    DEBUG: mail.imap.appendbuffersize: -1
    DEBUG: mail.imap.minidletime: 10
    DEBUG: disable AUTH=PLAIN

    DEBUG: trying to connect to host "imap.alibaba.com", port 143, isSSL false
    * OK AliYun IMAP Server Ready(10.200.217.123)
    A0 CAPABILITY
    * CAPABILITY IMAP4rev1 ID SASL-IR AUTH=XOAUTH
    A0 OK CAPABILITY completed
    DEBUG IMAP: AUTH: XOAUTH
    DEBUG: protocolConnect login, host=imap.alibaba.com, user=001@topchinagroup.cn, password=<non-null>
    DEBUG IMAP: LOGIN command trace suppressed
    DEBUG IMAP: LOGIN command result: A1 OK LOGIN completed
    A2 CAPABILITY
    * CAPABILITY IMAP4rev1 ID SASL-IR AUTH=XOAUTH
    A2 OK CAPABILITY completed
    DEBUG IMAP: AUTH: XOAUTH

    DEBUG: connection available -- size: 1
    A3 EXAMINE INBOX
    * 1000 EXISTS
    * 0 RECENT
    * OK [UNSEEN 977]
    * OK [UIDNEXT 32019] Predicted next UID
    * OK [UIDVALIDITY 2] UIDS valid
    * FLAGS (\Answered \Seen \Deleted \Draft)
    * OK [PERMANENTFLAGS (\Answered \Seen \Deleted \Draft)] Limited
    A3 OK [READ-WRITE] SELECT completed

    A4 UID FETCH 32019:* (UID)
    * 1000 FETCH (UID 32018)
    A4 OK FETCH completed

    A5 FETCH 1000 (BODYSTRUCTURE)
    javax.mail.FolderClosedException: * BYE JavaMail Exception: java.io.IOException: Connection dropped by server?
         at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1309)
         at com.sun.mail.imap.IMAPMessage.getContentType(IMAPMessage.java:450)
         at com.dhc.framework.mail.ParserMailEntityUtil.<init>(ParserMailEntityUtil.java:85)
         at com.dhc.maoyi365.mail.service.impl.MailReceiveAndDelServiceImpl.recMail(MailReceiveAndDelServiceImpl.java:409)
         at sun.reflect.GeneratedMethodAccessor84.invoke(Unknown Source)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:597)
         at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:296)
         at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
         at $Proxy1.recMail(Unknown Source)
         at com.dhc.maoyi365.mail.service.impl.bean.MailServerRecThread.runThread(MailServerRecThread.java:39)
         at com.dhc.framework.saas.thread.SaasBaseThread.run(SaasBaseThread.java)
         at java.lang.Thread.run(Thread.java:662)
    DEBUG: no connections in the pool, creating a new one
    * OK AliYun IMAP Server Ready(10.200.217.124)
    A0 CAPABILITY
    * CAPABILITY IMAP4rev1 ID SASL-IR AUTH=XOAUTH
    A0 OK CAPABILITY completed
    DEBUG IMAP: AUTH: XOAUTH
    DEBUG IMAP: LOGIN command trace suppressed
    DEBUG IMAP: LOGIN command result: A1 OK LOGIN completed
    A2 CAPABILITY
    * CAPABILITY IMAP4rev1 ID SASL-IR AUTH=XOAUTH
    A2 OK CAPABILITY completed
    DEBUG IMAP: AUTH: XOAUTH
    A3 EXAMINE INBOX
    * 1000 EXISTS
    * 0 RECENT
    * OK [UNSEEN 977]
    * OK [UIDNEXT 32019] Predicted next UID
    * OK [UIDVALIDITY 2] UIDS valid
    * FLAGS (\Answered \Seen \Deleted \Draft)
    * OK [PERMANENTFLAGS (\Answered \Seen \Deleted \Draft)] Limited
    A3 OK [READ-WRITE] SELECT completed
    javax.mail.FolderClosedException
         at com.sun.mail.imap.IMAPMessage.getProtocol(IMAPMessage.java:145)
         at com.sun.mail.imap.IMAPMessage.getMimeStream(IMAPMessage.java:674)
         at com.sun.mail.imap.IMAPMessage.writeTo(IMAPMessage.java:716)
         at com.dhc.framework.mail.ParserMailEntityUtil.<init>(ParserMailEntityUtil.java:99)
         at com.dhc.maoyi365.mail.service.impl.MailReceiveAndDelServiceImpl.recMail(MailReceiveAndDelServiceImpl.java:409)
         at sun.reflect.GeneratedMethodAccessor84.invoke(Unknown Source)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:597)
         at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:296)
         at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
         at $Proxy1.recMail(Unknown Source)
         at com.dhc.maoyi365.mail.service.impl.bean.MailServerRecThread.runThread(MailServerRecThread.java:39)
         at com.dhc.framework.saas.thread.SaasBaseThread.run(SaasBaseThread.java)
         at java.lang.Thread.run(Thread.java:662)
    java.lang.NullPointerException
         at com.dhc.maoyi365.mail.dao.MailServerDao.insertMailServerErrorlist(MailServerDao.java:330)
         at com.dhc.maoyi365.mail.service.impl.MailReceiveAndDelServiceImpl.recMailErrorLog(MailReceiveAndDelServiceImpl.java:227)
         at com.dhc.maoyi365.mail.service.impl.MailReceiveAndDelServiceImpl.recMail(MailReceiveAndDelServiceImpl.java:447)
         at sun.reflect.GeneratedMethodAccessor84.invoke(Unknown Source)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:597)
         at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:296)
         at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
         at $Proxy1.recMail(Unknown Source)
         at com.dhc.maoyi365.mail.service.impl.bean.MailServerRecThread.runThread(MailServerRecThread.java:39)
         at com.dhc.framework.saas.thread.SaasBaseThread.run(SaasBaseThread.java)
         at java.lang.Thread.run(Thread.java:662)
    A4 CLOSE
    A4 OK CLOSE completed
    DEBUG: added an Authenticated connection -- size: 1
    DEBUG IMAP: IMAPProtocol noop
    A5 NOOP
    A5 OK NOOP completed
    A6 LOGOUT
    * BYE IMAP4rev1 Server logging out
    A6 OK LOGOUT completed
    DEBUG: IMAPStore connection dead
    DEBUG: IMAPStore cleanup, force false
    DEBUG: IMAPStore cleanup done

    can you tell me the reason of these exception above mentioned? thank you very much!
  • 3. Re: java mail Store.connect hangs - doesn't timeout
    bshannon Pro
    Currently Being Moderated
    You want mail.imap.timeout.

    You'll get a MessagingException or an IOException (depending on the context) if the timeout occurs.

    The exception you're getting appears to be (as it says) because the server dropped the connection.
    There's nothing in the protocol trace to indicate why it dropped the connection, so perhaps the server
    crashed or run into some other server bug. You'll need to examine the log files in the server to find out.
  • 4. Re: java mail Store.connect hangs - doesn't timeout
    984643 Newbie
    Currently Being Moderated
    thank you very much for your answer.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points