java imap fetch messages since a date
Asked Answered
S

5

10

I am writing/learning to fetch email using java from an IMAP folder using javax.mail package. I was successfully able to retrieve the last n messages in a Folder, however I am looking to build an example to retrieve messages since a specified date. Any examples?

Saul answered 15/5, 2009 at 17:52 Comment(1)
Related: #6963756Freemason
A
26

You could also use the SearchTerm classes in the java mail package.

SearchTerm olderThan = new ReceivedDateTerm(ComparisonTerm.LT, someFutureDate);
SearchTerm newerThan = new ReceivedDateTerm(ComparisonTerm.GT, somePastDate);
SearchTerm andTerm = new AndTerm(olderThan, newerThan);
inbox.search(andTerm);

Some combination of the above should prove to be a better way to get dates within a certain range.

Amaris answered 15/5, 2009 at 20:11 Comment(2)
I am trying to use receivedDateTerm but seems it is not returning me correct result. I just want to retrieve all mails received in last 1 mins or all mails received from the time my test started. It is not working as expected.. this is what I am doing. SearchTerm st = new AndTerm(new SubjectTerm(subjectSearchPhrase), new BodyTerm(bodySearchPhrases)); st = new AndTerm(st, new ReceivedDateTerm(ComparisonTerm.GE, searchMailReceivedDateGE)); I have also tried using YoungerTerm(80) and this also doesn't work. any one knows the problem? thanks!Egon
One thing to note: IMAP can only search by sate, not time, so if one wants to get all new mails since the last update, they'll need to set the time of their comparison date back to midnight and do further checking against duplicates on the client.Sweeping
J
5
public class CheckDate  {
    public void myCheckDate(Date givenDate) {
        SearchTerm st = new ReceivedDateTerm(ComparisonTerm.EQ,givenDate);

        Message[] messages = inbox.search(st);
    }

    // in main method

    public static void main(String[] args) throws ParseException{
        SimpleDateFormat df1 = new SimpleDateFormat( "MM/dd/yy" );
        String dt="06/23/10";
        java.util.Date dDate = df1.parse(dt);
        cd.myCheckDate(dDate);
    }
}
Juicy answered 24/6, 2010 at 6:37 Comment(0)
I
2

Instead of fetching all messages you should try taking advantage of server side search. This works by using the search method of javax.mail.Folder. You will probably have to write your own SearchTerm based on a criteria on Message.getReceivedDate().

If server side search does not work, you could try using a fetch profile, i.e. instead of inbox.getMessages() use inbox.fetch(Message[] msgs, FetchProfile fp). The javadoc for fetch says: Clients use this method to indicate that the specified items are needed en-masse for the given message range. Implementations are expected to retrieve these items for the given message range in a efficient manner. Note that this method is just a hint to the implementation to prefetch the desired items.

Isochor answered 15/5, 2009 at 19:39 Comment(0)
S
0

Here is what I came up with. This works for me, but probably not the best way to go about it. Any suggestions to improve this?

      Date from; //assume initialized
      Store store; //assume initialized
      Folder inbox = store.getFolder("INBOX");
      inbox.open(Folder.READ_ONLY);
      int end = inbox.getMessageCount();
      long lFrom = from.getTime();
      Date rDate;
      long lrDate;
      int start = end;
      do {
        start = start - 10;
        Message testMsg = inbox.getMessage(start);
        rDate = testMsg.getReceivedDate();
        lrDate = rDate.getTime();
      } while (lrDate > lFrom);
      Message msg[] = inbox.getMessages(start, end);
      for (int i=0, n=msg.length; i<n; i++) {
        lrDate = msg[i].getReceivedDate().getTime();
        if (lrDate > lFrom) {
          System.out.println(i + ": "
            + msg[i].getFrom()[0]
            + "\t" + msg[i].getSubject());
        }
      }
Saul answered 15/5, 2009 at 18:24 Comment(1)
Definitely not a good approach as it fetches the emails first without filtering them with IMAP capabilities. I believe that the tenfold increment doesn't make it much nicer (imagine 10k emails in the box)Sirkin
I
0

All mails in the last month:

    Calendar cal = Calendar.getInstance();
    cal.roll(Calendar.MONTH, false);
    Message[] search = folder.search(new ReceivedDateTerm(ComparisonTerm.GT, cal.getTime()));
Irita answered 1/9, 2015 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.