vaibhav
vaibhav

Reputation: 4103

javax.mail.Message trying to fetch messages for a given date range

In my application i am trying to use java mail API to read through one mailbox where we recieve bounced email records, i believe we can fetch all the messages using

// Get a Store object that implements the specified protocol.
store = session.getStore(protocol);
//Connect to the current host using the specified username and password.
store.connect(hostName, userName, password);
folder = store.getFolder(folderName);
Message[] messages = folder.getMessages();

However this would return me all the messages in the provided folder, Is there a way where in i can find out the messages which i received yesterday on in a given date range.

Any help in this regard would be highly appreciated.

Thanks

Vaibhav

Upvotes: 2

Views: 5302

Answers (2)

vaibhav
vaibhav

Reputation: 4103

Following changes i did to make this work as per my expectations:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);

// We would get the bounce mails received yesterday

ReceivedDateTerm term  = new ReceivedDateTerm(ComparisonTerm.EQ,new Date(cal.getTimeInMillis()));

Message[] messages = folder.search(term)

Cheers! Vaibhav

Upvotes: 3

Bill Shannon
Bill Shannon

Reputation: 29961

See the Folder.search() method and the many search terms in the javax.mail.search package.

Note that IMAP searches are done on the server but only have resolution to days, not time. POP3 searches are done by downloading all messages to the client and searching there; probably not what you want to do.

Upvotes: 3

Related Questions