开发者

Finding a specific unread mail from list of mails using Javamail

开发者 https://www.devze.com 2023-03-24 02:42 出处:网络
MY approach is - Get the total mail list -开发者_Go百科 List<Message> totalMessageList = Arrays

MY approach is -

  1. Get the total mail list -

    开发者_Go百科
    List<Message> totalMessageList = Arrays
                .asList(folder.getMessages());
    
  2. Create a list out of this which has only unread mails. (I had to do this coz I could not find a any direct API to get list of new mails. One question which helped in finding whether a message is unread or not was posted here)

    List<Message> unreadMessageList = new ArrayList<Message>();
    

    For sake of brevity I have not posted entire logic of building the list of unread mail from list of total mails.

  3. Iterate through the list of unread mails > Check if any mail has desired subject then read content of mail > If no such mail found then throw IllegalStateExcepton

Now my question is - Could I improve on this approach?


Can you use

Folder.search(SearchTerm): Message[]

http://javamail.kenai.com/nonav/javadocs/javax/mail/Folder.html#search%28javax.mail.search.SearchTerm%29

with the relevant SearchTerm, e.g. FlagTerm for the unread flag

http://javamail.kenai.com/nonav/javadocs/javax/mail/search/FlagTerm.html

Quick search on Google found this

http://www.java2s.com/Code/Java/Email/Searchthegivenfolderformessagesmatchingthegivencriteria.htm

which might be useful, but I haven't tried it

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号