[ACCEPTED]-java imap fetch messages since a date-imap
You could also use the SearchTerm classes 3 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 2 of the above should prove to be a better 1 way to get dates within a certain range.
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);
}
}
0
Instead of fetching all messages you should 8 try taking advantage of server side search. This 7 works by using the search method of javax.mail.Folder. You will 6 probably have to write your own SearchTerm 5 based on a criteria on Message.getReceivedDate().
If 4 server side search does not work, you could 3 try using a fetch profile, i.e. instead 2 of inbox.getMessages() use inbox.fetch(Message[] msgs, FetchProfile 1 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.
Here is what I came up with. This works 3 for me, but probably not the best way to 2 go about it. Any suggestions to improve 1 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());
}
}
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()));
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.