Does JavaMail support server-push?

僤鯓⒐⒋嵵緔 提交于 2019-12-29 04:18:10

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/4389994/does-javamail-support-server-push

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!