Does JavaMail support server-push?

Timo picture Timo · Dec 8, 2010 · Viewed 7.1k times · Source

Does JavaMail support notification of new emails through server-push?

If yes, where is the documentation for that? If no, is there a library that can do it?

Answer

casey picture casey · Jan 7, 2011

You should be using IMAPFolder's idle function to issue the idle command to the server. That will then listen for events, such as a new mail or deleted mail. (See the IMAP spec to see what the messages look like). And you should be using a MessageCountListener to execute code when a number of emails in the mailbox change.

IMAP's idle function is exactly meant to imitate "push" functionality.

http://java.sun.com/products/javamail/javadocs/javax/mail/event/MessageCountListener.html
http://java.sun.com/products/javamail/javadocs/com/sun/mail/imap/IMAPFolder.html

Sorry I didn't post any code that shows how this is used. I didn't want to waste my time since there are many readily available examples on the internet if you search for this stuff.

But be forewarned, this method won't work for more than one IMAP account since the idle command blocks. Unless you want them all on different threads (bad idea).