Use Inbox Listeners Carefully
JavaMail
allows you to assign a listener to a message store, assuming that the
server supports adding new messages while a user is connected. IMAP
does, but POP3 does not. To be notified when a new message arrives,
create a new class implementing
MessageCountListener
and associate it with a
Folder
object. The messagesAdded( )
method will be called whenever messages are added to the folder.
In most real-life situations this isn’t a great way
to go. It requires keeping the connection to the server open, which
consumes network resources. The mail server might decide to time you
out, or a network problem could knock out the connection. Some
servers place a limit on the amount of time one user can be
connected. So if you take this approach, implement the
ConnectionListener
interface and associate it with the folder and message store objects.
This will allow you to recreate the connection and folder and assign
a new listener.