Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JavaMail support server-push?

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?

like image 683
Timo Ernst Avatar asked Dec 08 '10 16:12

Timo Ernst


People also ask

What is JavaMail used for?

The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform.

What is SMTP server in Java?

SMTP is an acronym for Simple Mail Transfer Protocol. It is an Internet standard for electronic mail (e-mail) transmission across Internet Protocol (IP) networks. SMTP uses TCP port 25.

Which package is required for mailing services in Java?

The javax. mail. internet package defines classes that are specific to mail systems based on internet standards such as MIME, SMTP, POP3, and IMAP.


2 Answers

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).

like image 57
casey Avatar answered Oct 28 '22 15:10

casey


A Store event listens for notifications issued by your backend store:

http://java.sun.com/products/javamail/javadocs/javax/mail/event/StoreEvent.html

But in my experience the java mail docs are so thin in places, that the best way of finding out what is going on, is to debug through the process yourself.

This is a great allround resource as well; the JavaMail FAQ :

http://www.oracle.com/technetwork/java/faq-135477.html

like image 26
davek Avatar answered Oct 28 '22 14:10

davek