javamail将gmail消息标记为已读 [英] javamail mark gmail message as read

查看:715
本文介绍了javamail将gmail消息标记为已读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:回答后添加:
谢谢..是的,我曾尝试过Flag.SEEN为true和saveChanges ..我也读过了读取的getContent标记。我尝试在循环消息的for语句中使用它。但是我在下一个循环中再次从文件夹中获取了消息。我假设文件夹是实时的,所以抓住内容,然后从过滤器的文件夹中再次抓取消息,以便没有看到应该工作,但我仍然得到相同的消息。我可以尝试关闭文件夹并重新打开作为测试,以查看它是否已标记。此外,如果我转到我的客户端并单击该消息,那么我的代码即使在循环中也会停止查看它,所以我希望在代码中也这样做。



原始:
我正在使用javamail从gmail帐户获取电子邮件,它运行良好,当我收到消息时我想将其标记为已阅读,是否有人可以给我一些指示?这是我目前的代码:

 属性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);
//获取文件夹
文件夹文件夹= store.getFolder(INBOX);
if(folder == null ||!folder.exists()){
return null;
}
folder.open(Folder.READ_ONLY);

//只拉未读
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN),false);
消息消息[]; // = folder.search(ft);

for(int x = 0; x< timeOutInSeconds; x ++){
log.reportMessage(查找电子邮件);
try {
folder.getMessages();
messages = folder.search(ft);

if(messages.length> 0){
for(Message message:messages){
//log.reportMessage(\"found message:不应该再看到,标记读);
//想要标记为读取

}
}
Thread.sleep(1000);
}
catch(exception ex){

}
}

//关闭连接
folder.close(false );
store.close();
返回null;

}
catch(NoSuchProviderException ex){

返回null;
}
catch(MessagingException ex){

返回null;
}


}


解决方案

首先,如果您使用的是POP3服务器,则无法将邮件标记为已读 - POP3协议不支持该邮件。但是,IMAP v4协议可以。



您可能认为这样做的方法是获取消息,设置 Flags.Flag.SEEN 标志为true,然后调用 message.saveChanges()。奇怪的是,情况并非如此。



相反,JavaMail API设计规范第4章标志类中指出 SEEN检索邮件内容时隐式设置标志。因此,要将邮件标记为已读,您可以使用以下代码:

  myImapFolder.open(Folder.READ_WRITE); 
myImapFolder.getMessage(myMsgID).getContent();
myImapFolder.close(false);

或者另一种方法是使用MimeMessage复制构造函数,即:

  MimeMessage source =(MimeMessage)folder.getMessage(1)
MimeMessage copy = new MimeMessage(source);

构造副本时,将为源引用的消息隐式设置seen标志。 / p>

Note: added after answer: Thanks.. Yeah I had tried the Flag.SEEN to true and saveChanges.. I also had read getContent marks it read. I tried using it in the for statement that loops through the messages. But I got the messages again from the folder anyways in the next loop. I was assuming the folder was live, so grabbing the content, then grabbing the messages again from the folder with the filter to not get any seen should work, but I was still getting the same message. I could try closing the folder and reopen as a test to see if it's marked. Also if I go over to my client and click the message, then my code stops seeing it even in the loop, so I was hoping to do the same in the code.

original: I'm using javamail to get email from a gmail account, it's working great, when I get the message I'd like to mark it as read, can anyone give me some direction? Here is my current code:

    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;
    }


}

解决方案

First of all, you can't mark a message as read if you are using a POP3 server - the POP3 protocol doesn't support that. However, the IMAP v4 protocol does.

You might think the way to do this is to get the message, set the Flags.Flag.SEEN flag to true, and then call message.saveChanges(). Oddly, this is not the case.

Instead, the JavaMail API Design Specification, Chapter 4, section "The Flags Class" states that the SEEN flag is implicitly set when the contents of a message are retrieved. So, to mark a message as read, you can use the following code:

myImapFolder.open(Folder.READ_WRITE);
myImapFolder.getMessage(myMsgID).getContent();
myImapFolder.close(false);

Or another way is to use the MimeMessage copy constructor, ie:

MimeMessage source = (MimeMessage) folder.getMessage(1)
MimeMessage copy = new MimeMessage(source);

When you construct the copy, the seen flag is implicitly set for the message referred to by source.

这篇关于javamail将gmail消息标记为已读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆