JavaMail - Pop3Folder意外关闭

时间:2012-06-14 19:39:15

标签: glassfish javamail

成功打开Pop3Folder并从中检索邮件后,当folder.isOpen返回false时,我有时会说到这一点。同时,在调试模式下查看Pop3Folder的字段时,我发现字段opened设置为true

有人可以给我一个暗示,这里可能出现什么问题?

以下是代码:

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 个答案:

答案 0 :(得分:1)

如果您的processMessage方法花费的时间太长,服务器可能会超时连接。打开Session调试并检查协议跟踪以获取线索。