Java Reference
In-Depth Information
CHAPTER 1
Introducing the JavaMail API
The JavaMail API is a fairly high-level representation of the basic components of any
email system. The components are represented by abstract classes in the javax.mail
package. For instance, the abstract class javax.mail.Message represents an email mes‐
sage. It declares abstract methods to get and set various kinds of envelope information
for the message, such as the sender and addressee, the date sent, and the subject. The
abstract class javax.mail.Folder represents a message container. It declares abstract
methods to retrieve messages from a folder, move messages between folders, and delete
messages from a folder.
These classes are all abstract because they don't make many assumptions about how the
email is stored or transferred between machines. For instance, they do not assume that
messages are sent using SMTP or that they're structured as specified in RFC 822. Con‐
crete subclasses of these classes specialize the abstract classes to particular protocols and
mail formats. If you want to work with standard Internet email, you might use jav
ax.mail.MimeMessage instead of javax.mail.Message , javax.mail.InternetAd
dress instead of javax.mail.Address , and com.sun.mail.imap.IMAPStore instead of
javax.mail.Store . If you were writing code for a Microsoft Exchange-based system,
you'd use different concrete implementation classes but the same abstract base classes.
The JavaMail API roughly follows the abstract factory design pattern. This pattern al‐
lows you to write your code based on the abstract superclasses without worrying too
much about the lower-level details. The protocols and formats used and the associated
concrete implementation classes are determined mostly by one line of code early in the
program that names the protocol. Changing the protocol name goes 90% of the way
toward porting your program from one protocol (say, POP) to another (say, IMAP).
Service providers implement particular protocols. A service provider is a group of con‐
crete subclasses of the abstract JavaMail API classes that specialize the general API to a
particular protocol and mail format. These subclasses are probably (though not
 
Search WWH ::




Custom Search