2012-06-14 93 views
0

成功打開Pop3Folder並從中檢索消息後,我有時會回到這一點,當folder.isOpen返回false。同時,在調試模式下查看Pop3Folder的字段時,我看到字段opened設置爲trueJavaMail - Pop3Folder意外關閉

有人能給我一個提示,這裏可能會出錯嗎?

下面是代碼:

public void popMail(MessageProcessor messageProcessor) throws MessagingException { 
    Folder inboxFolder = null; 
    Store store = null; 
    try { 
    store = mailSession.getStore(); 
    store.connect(mailSession.getProperty("mail.user"), 
     mailSession.getProperty("mail.password")); 
    // OK. Connected to POP3 Store. 
    inboxFolder = store.getFolder("inbox"); 
    inboxFolder.open(Folder.READ_WRITE); 
    // The folder is successfully opened. 
    Message[] msgs = inboxFolder.getMessages(); 
    // Messages are successfully retrieved. 
    if (msgs != null && msgs.length > 0) { 
     for (Message msg : msgs) { 
     if (messageProcessor != null) { 
      // Calling custom listener to process message 
      messageProcessor.processMessage(msg); 
     } 
     msg.setFlag(Flag.DELETED, true); 
     } 
    } 
    } finally { 
    // Oops, inboxFolder.isOpen returns false. 
    // Meanwhile I see in debug mode that inboxFolder#opened is set to true 
    if (inboxFolder != null && inboxFolder.isOpen()) { 
     try { 
     inboxFolder.close(true); 
     } catch (MessagingException e) { 
     log.warn("Error while closing folder"); 
     } 
    } if (store != null) { 
     try { 
      store.close(); 
     } catch (MessagingException e) { 
      log.warn("Error while closing store"); 
     } 
     } 
    } 
} 

回答

1

服務器可能超時的連接,如果你的方法,而processMessage時間過長。打開會話調試並檢查協議跟蹤以尋找線索。

+0

謝謝,我會看看這個。 – jFrenetic

+0

我沒有打開調試,但我可以肯定地說,在處理消息大約2分鐘後,文件夾最終會被關閉。所以是的,這很可能是服務器超時連接。再次感謝! – jFrenetic