如何知道消息是否被读取

时间:2012-06-27 07:10:19

标签: java javax.mail

我正在理解javax.mail阅读电子邮件帐户并收到所有收到的邮件。 这是一个例子:

Properties properties = System.getProperties();  
         properties.setProperty("mail.store.protocol", "imaps");  
             try {  
                 Session session = Session.getDefaultInstance(properties, null);
                 //create session instance
                 Store store = session.getStore("imaps");//create store instance  
                 store.connect("pop.gmail.com", "mail@gmail.com", "***");  
                 //set your user_name and password 
                 System.out.println(store);   
                 Folder inbox = store.getFolder("inbox");
                 //set folder from where u wants to read mails  
                 inbox.open(Folder.READ_ONLY);//set access type of Inbox  
                 Message messages[] = inbox.getMessages();// gets inbox messages

                 for(Message message:messages) {
                 System.out.println(message.getContent().toString());  
             }
                 store.close();
              } catch (Exception e) {  
             System.out.println(e);    
         }  

我的问题是:

Message messages[]可以知道邮件是否是新邮件?类似的东西:

for(Message message: messages){
   if(message.isNew()) OR if(!message.isRead()){
      System.out.println("Message is not still read");
   }
}

有人能帮助我吗?感谢!!!

1 个答案:

答案 0 :(得分:5)

将其放入您的代码中

Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_ONLY);
// search for all "unseen" messages
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
Message messages[] = inbox.search(unseenFlagTerm);


if (messages.length == 0) System.out.println("No messages found.");

可以找到一个非常好的教程here