如here here和here所述,应该少用或少使用Java Mail API的getInstance()方法,以正确设置属性。
所以让我们检查一下我的代码:
[...]
Properties properties = new Properties();
properties.put("mail.store.protocol", "imaps");
properties.put("mail.imap.port", "143");
properties.put("mail.imap.ssl.enable", "true");
properties.put("mail.imaps.partialfetch", "false");
properties.put("mail.mime.base64.ignoreerrors", "true");
Session session = Session.getInstance(properties);
session.setDebug(true);
Store store = session.getStore("imaps");
store.connect(host, userName, password);
Folder imapFolder = store.getFolder(folderName);
imapFolder.open(Folder.READ_WRITE);
Message[] messages = imapFolder.search(new FlagTerm(new
lags(Flags.Flag.SEEN), false));
[...]
看到我的端口属性设置为“ 143”吗?让我们跳到邮件api调试控制台输出:
DEBUG: setDebug: JavaMail version 1.5.6
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.partialfetch: false
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: closeFoldersOnStoreFailure
DEBUG IMAPS: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from 213.189.150.34 r25-v6mb373781038edd
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH
A0 OK Thats all she wrote! r25-v6mb373781038edd
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: PLAIN-CLIENTTOKEN
DEBUG IMAPS: AUTH: OAUTHBEARER
DEBUG IMAPS: AUTH: XOAUTH
DEBUG IMAPS: protocolConnect login, host=imap.gmail.com, user="myuser", password="mypassword"
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 OK "myuser" 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 LIST-EXTENDED LIST-STATUS LITERAL- SPECIAL-USE APPENDLIMIT=35651584
A2 OK Success
DEBUG IMAPS: connection available -- size: 1
A3 SELECT WhiteListedMembers
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)
* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing \*)] Flags permitted.
* OK [UIDVALIDITY 12] UIDs valid.
* 379 EXISTS
* 0 RECENT
* OK [UIDNEXT 386] Predicted next UID.
* OK [HIGHESTMODSEQ 65275]
A3 OK [READ-WRITE] WhiteListedMembers selected. (Success)
A4 SEARCH UNSEEN ALL
* SEARCH
A4 OK SEARCH completed (Success)
Unreaded messages in inbox: 0
A5 UNSELECT
A5 OK Returned to authenticated state. (Success)
DEBUG IMAPS: added an Authenticated connection -- size: 1
A6 LOGOUT
我的问题是:为什么Java邮件api仍然使用端口993?是否有自动端口映射? 我只是不明白这一点。
有人可以带上光线吗?
非常感谢您的回答和赞助时间。
最好的问候 马特
OS:Windows 10,Eclipse Oxygen 4.7.0,Java Oracle JRE 1.8.0 131,Java Mail API 1.5.6
答案 0 :(得分:1)
您是否真的在端口143上运行带SSL的IMAP?充其量是“不寻常的”。
如果使用默认端口,则无需设置端口属性。只需使用“ imap”协议并设置“ mail.imap.ssl.enable”属性,JavaMail将选择正确的默认端口。