Receiving email using Imap through SSL connection using javax.mail

蓝咒 提交于 2019-12-06 07:34:11

Setting various socketFactory properties. Long, long ago JavaMail didn't have built in support for SSL connections, so it was necessary to set these properties to use SSL. This hasn't been the case for years; remove these properties and simplify your code. The easiest way to enable SSL support in current versions of JavaMail is to set the property "mail.smtp.ssl.enable" to "true". (Replace "smtp" with "imap" or "pop3" as appropriate.) https://javaee.github.io/javamail/FAQ#commonmistakes

String host = "mail.example.com";
String username = "email@example.com";
String password = "mysecretpassword";

Properties props = new Properties();
props.setProperty("mail.imap.ssl.enable", "true");

Session session = javax.mail.Session.getInstance(props);
Store store = session.getStore("imap");
store.connect(host, username, password);

Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();


inbox.close(false);
store.close();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!