Java 邮件:如何识别新邮件

Java Mail: How to Recognize New Mails

当我收到邮件时,打开收件箱,收到邮件;我怎样才能将它们与之前的一系列消息进行比较,以便我知道我已经阅读了其中的哪些内容?

Properties properties = new Properties();
properties.put("mail.imaps.host", host);
properties.put("mail.imaps.port", "993");
Session emailSession = Session.getDefaultInstance(properties);

//2) create the POP3 store object and connect with the pop server
Store emailStore = emailSession.getStore("imaps");
emailStore.connect(user,pw);

//3) create the folder object and open it
Folder emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();

具体如何处理取决于您的要求。例如,其他应用程序 and/or 用户是否会与您的应用程序同时访问邮箱?或者您的应用程序是否具有对邮箱的专用访问权限?

你可以use the SEEN flag to find messages that you haven't already read.

您还可以保存上次处理的邮件的 IMAP UID(以及文件夹的 UIDVALIDITY),以便以后查找具有更大 UID 的邮件。详情见UIDFolder界面

(您的代码中的注释讨论了 pop,但您的代码使用的是 imap;如果您必须使用 pop3 协议,answers are quite different。)