2012-06-27 55 views
1

我正在理解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", "[email protected]", "***"); 
       //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"); 
    } 
} 

有人能幫助我嗎?謝謝!!!

+0

看到其他的問題(你稍有不同,OP代碼演示瞭如何的完成http://stackoverflow.com/ questions/5366767/retrieve-unread-emails-from-gmail-javamail-api-imap) – BigMike

回答

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

+0

謝謝!!! :D我看過你的答案之前看過那個教程:D – JackTurky

相關問題