title: Reading emails with groovy (Java Mail)
body: <p>I am using groovy in order to access gmail and read the Inbox. It is regular JavaMail and will not describe it here.</p>
  
  <p>So for simplicity, after I connect to the store, I use this:</p>
  
  <pre><code>folder.open(Folder.READ_ONLY)
  
   folder.messages.each { msg -&gt;
      ...
      doSomething with msg
  ...
  }
  </code></pre>
  
  <p>this is working fine.</p>
  
  <p>However I have a performance issue. Sometimes messages[] could be big. Some folders contain more than 1000 messages, and checking them all takes time.</p>
  
  <p>I am looking for a quicker way to get only those emails that are the most recent (for example messages from the last 5 days or something like that)</p>
  
  <p>of course I have the date information in each msg and I could do my comparison, but this is not efficient since it will loop through the entire collection.</p>
  
  <p>Is there a better way to get those messages?</p>