Can't use SearchTerm term = new MessageIDTerm(ID) to search mail by message-ID

好久不见. 提交于 2019-12-14 04:22:55

问题


Iam developing an Email application on Android and I got stuck at here. If I use Gmail server I can search Email by ID

  SearchTerm term = new MessageIDTerm(ID);
  message_s = folder.search(term);

But with this code, I can't search email with some Other server like yahoo,hotmail. And I try solve this bug by override this code

SearchTerm messageIDTerm= new SearchTerm() {
@Override
 public boolean match(Message message) {
 try {
    String   messageID="";
Enumeration headers = message.getAllHeaders();

   while (headers.hasMoreElements()) {
    eader h = (Header) headers.nextElement();               
    String mID = h.getName();                
    if(mID.contains("Message-ID") || mID.contains("Message-Id")){
    messageID= h.getValue();
    }
    }
    if (messageID.contains(ID)) {
     return true;
    }
    } catch (MessagingException ex) {
    ex.printStackTrace();
    }
    return false;
    }
    };
   message_s = folder.search(messageIDTerm);

It's success to search mail. But the problem is this code check every Message-ID by client search(download messeage-ID and compare) and it take a lot of time to find the result.

The first way, server make a seach for my request and its very fast.

So how do I make the search as fast as the first way if I use the second way?


回答1:


Some servers are broken. You can't fix them.

If you have to do the search in the client, the best you can do is use the Folder.fetch method to prefetch the headers you're going to need to do the search. Then use the standard MessageIDTerm with the Message.match method as you iterate over all the messages you need to check.



来源:https://stackoverflow.com/questions/19486117/cant-use-searchterm-term-new-messageidtermid-to-search-mail-by-message-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!