Chapter 9. Event Handling

Many email programs can periodically check for incoming email in the background. One way to structure an email program is as a series of responses to unpredictable events. This is much like programming for a graphical user interface, and indeed the JavaMail API uses the same Observer pattern to handle mail events that JavaBeans and Swing use to handle GUI events.

The JavaMail API defines six different kinds of mail events, all in the javax.mail.event package. They are all subclasses of MailEvent:

public abstract class MailEvent extends EventObject

The six concrete kinds of mail events are:

A Folder, Store, or Transport has been opened, closed, or disconnected.
A Folder has been created, deleted, or renamed.
The message’s envelope or flags have changed.
A message was added to or deleted from a Folder.
A notification or alert from a Store.
A notification from a Transport that a message was delivered, partially delivered, or failed to be delivered.

There are six listener interfaces corresponding to the six kinds of events:

public interface ConnectionListener     extends EventListener
public interface FolderListener         extends EventListener
public interface MessageChangedListener extends EventListener
public interface MessageCountListener   extends EventListener
public interface StoreListener          extends EventListener
public interface TransportListener      extends EventListener ...

Get JavaMail API now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.