Search code examples
javajakarta-mailimapgmail-imap

How to get email messages not older than X seconds?


I use this code to search through my Gmail inbox:

Session session = Session.getDefaultInstance(new Properties());
Store store = session.getStore("imaps");
store.connect("imap.googlemail.com", 993, EMAIL, PASSWORD);
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);
Message[] messages = emailFolder.search(new AndTerm(new SearchTerm[] { new SubjectTerm(subject), new FromTerm(new InternetAddress(sender)), new YoungerTerm((int)TimeUnit.MINUTES.toSeconds(3)) }));

But I get this error:

javax.mail.MessagingException: A4 BAD Could not parse command;

If I remove the YoungerTerm search term, the search call succeeds and returns messages, but I need to reduce the results by adding the YoungerTerm filter.


Solution

  • The YoungerTerm class depends on an IMAP extension gmail does not support, RFC 5032. The only way to do what you want is to retrieve the INTERNALDATE and filter in the client.

    A bit strange that javamail will let you create and send a command that uses YoungerTerm; surely it ought to know that the server will reject it?