title: Can I perform a search on mail server in Java?
body: <p>I am trying to perform a search of my gmail using Java. With JavaMail I can do a message by message search like so:</p>
  
  <pre><code>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]);
  </code></pre>
  
  <p>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...).</p>
  
  <p>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?</p>
  
  <p>Hours of Googling has turned up nothing.</p>