javamail 将 gmail 邮件标记为已读
注意:回答后添加:谢谢.是的,我试过了 Flag.SEEN to true 并保存更改..我也读过getContent标记它读。我尝试在循环访问消息的for语句中使用它。但是,无论如何,在下一个循环中,我还是从文件夹中再次收到了消息。我假设该文件夹是实时的,因此抓取内容,然后使用过滤器从文件夹中再次抓取邮件以使其看不到任何内容应该有效,但我仍然收到相同的邮件。我可以尝试关闭文件夹并重新打开作为测试,以查看它是否被标记。此外,如果我转到我的客户端并单击该消息,那么即使在循环中,我的代码也会停止看到它,所以我希望在代码中执行相同的操作。
原创:我正在使用javamail从gmail帐户获取电子邮件,它工作得很好,当我收到我想将其标记为已读的消息时,任何人都可以给我一些指导吗?这是我当前的代码:
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", eUserName, ePassWord);
// Get folder
Folder folder = store.getFolder("INBOX");
if (folder == null || !folder.exists()) {
return null;
}
folder.open(Folder.READ_ONLY);
// Only pull unread
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message messages[]; // = folder.search(ft);
for(int x = 0; x < timeOutInSeconds; x++) {
log.reportMessage("looking for emails");
try {
folder.getMessages();
messages = folder.search(ft);
if (messages.length > 0) {
for (Message message : messages) {
//log.reportMessage("found message: should not see again, marking read");
// want to mark as read
}
}
Thread.sleep(1000);
}
catch(Exception ex) {
}
}
// Close connection
folder.close(false);
store.close();
return null;
}
catch (NoSuchProviderException ex) {
return null;
}
catch (MessagingException ex) {
return null;
}
}