我只是简单地尝试阅读收件箱中的所有邮件,然后分类为已读,未读和大于或等于日期术语。请在下面查看我的代码。用户名和密码部分已通过。
通常代码非常简单,但我不明白为什么我只收到所有电子邮件。
同时,我确信已阅读消息。此外,电子邮件收到的日期不同,我们有新的电子邮件。
我没有例外或错误。
还在下面检查了几个问题。
使用 javax.mail 阅读最近和未见过的消息
Properties props = new Properties();
props.put("mail.host", "outlook.office365.com");
props.put("mail.store.protocol", "pop3s");
props.put("mail.pop3s.auth", "true");
props.put("mail.pop3s.port", "995");
Session session = Session.getInstance(props, new avax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, passwd);
}
});
Security.setProperty("ssl.SocketFactory.provider", "com.ibm.jsse2.SSLSocketFactoryImpl");
//Security.setProperty("ssl.ServerSocketFactory.provider", "com.ibm.jsse2.SSLServerSocketFactoryImpl");
Store store = session.getStore("pop3s");
store.connect();
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Calendar myCal = new GregorianCalendar();
myCal.setTime(new Date());
myCal.add(Calendar.DAY_OF_MONTH, -1);
// retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
System.out.println("ALL : " + messages.length);
messages = emailFolder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
//messages = emailFolder.search(new FlagTerm(new Flags(Flag.SEEN), false)); // not working also
System.out.println("UNREAD : " + messages.length);
messages = emailFolder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), true));
//messages = emailFolder.search(new FlagTerm(new Flags(Flag.SEEN), true)); // not working also
System.out.println("READ : " + messages.length);
messages = emailFolder.search(new ReceivedDateTerm(ComparisonTerm.GE, myCal.getTime()));
System.out.println("ReceivedDateTerm : " + messages.length);
结果是; 全部 : 18 未读 : 18 读取 : 0 接收日期期限 : 0
谢谢
如 com.sun.mail.pop3 包的 javadocs 中所述,POP3 协议不支持永久标志,因此无法判断消息是最近还是已见。 请改用 imap。
此外,您需要修复这些常见的JavaMail错误。