我正在编写一个连接到gmail的程序,并检查收件箱中的邮件数量。如果我在未打开的getMessageCount()
上调用Folder
,则会返回554,但在打开后会返回541.为什么会有区别?
顺便说一句,我最终会尝试检查已删除的邮件(尚未清除)并尽可能恢复它们。
编辑一些示例代码:
Session session = Session.getDefaultInstance(System.getProperties());
Store store = session.getStore("imaps");
store.connect("imap.googlemail.com", 993, email, password);
Folder inbox = store.getFolder("INBOX");
System.out.println(inbox.getMessageCount());
inbox.open(Folder.READ_ONLY);
System.out.println(inbox.getMessageCount());
编辑:协议跟踪:
(与此同时,我收到了更多电子邮件,因此数字与上述不同)
DEBUG: setDebug: JavaMail version 1.5.2
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: trying to connect to host "imap.googlemail.com", port 993, isSSL true
* OK Gimap ready for requests from <ip> vt17mb17567619iec
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN
A0 OK Thats all she wrote! vt17mb17567619iec
DEBUG IMAPS: AUTH: XOAUTH
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: PLAIN-CLIENTTOKEN
DEBUG IMAPS: protocolConnect login, host=imap.googlemail.com, user=<email>, password=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 OK <email> authenticated (Success)
A2 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT
A2 OK Success
A3 LIST "" INBOX
* LIST (\HasChildren) "/" "INBOX"
A3 OK Success
A4 STATUS INBOX (MESSAGES RECENT UNSEEN UIDNEXT UIDVALIDITY)
* STATUS "INBOX" (MESSAGES 585 RECENT 0 UIDNEXT 3389 UIDVALIDITY 2 UNSEEN 0)
A4 OK Success
585
DEBUG IMAPS: connection available -- size: 1
A5 EXAMINE INBOX
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $Phishing $Forwarded NonJunk $NotPhishing Junk)
* OK [PERMANENTFLAGS ()] Flags permitted.
* OK [UIDVALIDITY 2] UIDs valid.
* 548 EXISTS
* 0 RECENT
* OK [UIDNEXT 3389] Predicted next UID.
* OK [HIGHESTMODSEQ 687472]
A5 OK [READ-ONLY] INBOX selected. (Success)
548