我可以在 Java 中对邮件服务器执行搜索吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2406068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:59:33  来源:igfitidea点击:

Can I perform a search on mail server in Java?

javaemailsearchexchange-serverjavamail

提问by smurthas

I am trying to perform a search of my gmail using Java. With JavaMail I can do a message by message search like so:

我正在尝试使用 Java 搜索我的 gmail。使用 JavaMail,我可以通过消息搜索来发送消息,如下所示:

Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "myUsername", "myPassword");

Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);

SearchTerm term = new SearchTerm() {
  @Override
  public boolean match(Message mess) {
    try {
      return mess.getContent().toString().toLowerCase().indexOf("boston") != -1;
    } catch (IOException ex) {
      Logger.getLogger(JavaMailTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (MessagingException ex) {
      Logger.getLogger(JavaMailTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
  }
};

Message[] searchResults = inbox.search(term);
for(Message m:searchResults)
  System.out.println("MATCHED: " + m.getFrom()[0]);

But this requires downloading each message. Of course I can cache all the results, but this becomes a storage concern with large gmail boxes and also would be very slow (I can only imagine how long it would take to search through gigabytes of text...).

但这需要下载每条消息。当然,我可以缓存所有结果,但这会成为大型 gmail 邮箱的存储问题,而且速度会非常慢(我只能想象搜索千兆字节的文本需要多长时间......)。

So my question is, is there a way of searching through mail on the server, a la gmail's search field? Maybe through Microsoft Exchange?

所以我的问题是,有没有办法在服务器上搜索邮件,就像 gmail 的搜索字段一样?也许通过 Microsoft Exchange?

Hours of Googling has turned up nothing.

数小时的谷歌搜索一无所获。

回答by Martin

You can let the server do the search for you, with the appropriate IMAP command. The SEARCH command will only get you so far, what you probably need is the SORT command. SORT isn't implemented in JavaMail but the documentationshows how you can implement it yourself:

您可以使用适当的 IMAP 命令让服务器为您进行搜索。SEARCH 命令只能让您到此为止,您可能需要的是 SORT 命令。JavaMail 中没有实现 SORT,但文档显示了如何自己实现它:

http://java.sun.com/products/javamail/javadocs/com/sun/mail/imap/IMAPFolder.html#doCommand(com.sun.mail.imap.IMAPFolder.ProtocolCommand)

http://java.sun.com/products/javamail/javadocs/com/sun/mail/imap/IMAPFolder.html#doCommand(com.sun.mail.imap.IMAPFolder.ProtocolCommand)

(I couldn't figure out how to link to a URL with parentheses)

(我不知道如何链接到带括号的 URL)

回答by jqa

Connect to the Exchange IMAP store and use javax.mail.search.SearchTerm

连接到 Exchange IMAP 存储并使用 javax.mail.search.SearchTerm