使用IMAP协议开发网易邮箱接收邮件时会出现以下报错:
javax.mail.MessagingException: B3 NO EXAMINE Unsafe Login. Please contact kefu@188.com for help;\n nested exception is:\n\tcom.sun.mail.iap.CommandFailedException: B3 NO EXAMINE Unsafe Login. Please contact kefu@188.com for help
百度了大多数答案,都是改设置,均不靠谱,最后自己找到了解决方法:连接服务器时带上IMAP ID信息。相关Java代码如下:
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.host", "imap.163.com");
props.setProperty("mail.imap.port", "143");
HashMap IAM = new HashMap();
//带上IMAP ID信息,由key和value组成,例如name,version,vendor,support-email等。
IAM.put("name","myname");
IAM.put("version","1.0.0");
IAM.put("vendor","myclient");
IAM.put("support-email","testmail@test.com");
Session session = Session.getInstance(props);
IMAPStore store = (IMAPStore) session.getStore("imap");
//下方替换对应帐号和授权码
store.connect("testmail@163.com", "password");
store.id(IAM);
完整代码可参考这篇文章,还介绍了如何监听接收邮件:IMAP协议定时监听接收邮件(QQ邮箱、网易邮箱都可)