我可以在Java中的邮件服务器上执行搜索吗? [英] Can I perform a search on mail server in Java?

查看:93
本文介绍了我可以在Java中的邮件服务器上执行搜索吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java搜索我的gmail.借助JavaMail,我可以像这样通过消息搜索来发送消息:

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

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]);

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

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...).

所以我的问题是,有没有一种方法可以通过服务器上的邮件(la gmail的搜索字段)进行搜索?也许通过Microsoft Exchange?

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?

谷歌搜索小时没有发现任何事情.

Hours of Googling has turned up nothing.

推荐答案

您可以使用适当的IMAP命令让服务器为您搜索. SEARCH命令只会帮助您解决问题,您可能需要的是SORT命令. SORT未在JavaMail中实现,但文档显示了如何自己实现:

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 documentation shows how you can implement it yourself:

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

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

这篇关于我可以在Java中的邮件服务器上执行搜索吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆