Understanding JavaMail

JavaMail is a unified service for abstracting an electronic mail (e-mail) system. It has pre-built implementations of some of the most popular protocols for mail transfer and provides an easy way to use them. The JavaMail API is designed to provide protocol-independent access for sending and receiving messages. However, it does not include any facilities for adding, removing, or changing user accounts. There are no standards to accomplish these tasks, and every e-mail system handles them differently.

You probably already know the most common e-mail agents (clients), such as Eudora, Outlook, Netscape, and other modern e-mail clients, which let you send both text and HTML e-mails. E-mail messages were originally limited to plain text and they did not support bold, italics, or hyperlinks. Modern e-mail agents recognize HTML, so you can now send either plain text messages or rich-content documents languages that aren't Latin based, such as Japanese and Chinese.

The JavaMail API provides a set of abstract classes and interfaces that comprise an electronic mail system. The abstract classes and interfaces support many different implementations of message stores, formats, and transports. Many simple applications will need to interact with the messaging system only through these base classes and interfaces.

Note

JavaMail helps create an e-mail agent (or mail client), and does not provide any mail server functionality. So, you must have access to a mail server before you try using JavaMail.


The abstract classes in the JavaMail API are expandable, and they can be subclassed to provide new protocols and add new functionality when necessary. In addition, JavaMail API includes concrete subclasses that implement the most widely used Internet mail protocols, such as Internet Message Access Protocol (IMAP), Post Office Protocol (POP), and Simple Mail Transfer Protocol (SMTP). They are ready to be used in application development. Developers can subclass JavaMail classes to provide the implementations of particular messaging systems, other than these protocols.

JavaMail Architecture

JavaMail is designed to standardized access to a variety of e-mail services. The JavaMail architecture provides a protocol-independent access for sending and receiving messages. This abstract mechanism is similar to other J2EE APIs, such as JDBC, JNDI, and JMS. Similarly, the JavaMail API is divided into two main parts: an application-independent part, and a service-dependent part. Your applications are written in a standard way to use the application-independent part of the JavaMail API, which transparently calls the underlying protocol or e-mail service. A JavaMail-compliant service must implement part of the JavaMail API. Here is a brief description of the two parts that comprise the JavaMail architecture:

  • An application-programming interface (API): This API is used by the application components to send and receive mail messages, independent of the underlying provider or protocol used.

  • A service provider interface (SPI): This part of the API speaks the protocol-specific languages, such as SMTP, POP, IMAP, and Network News Transfer Protocol (NNTP). It is used to plug in a provider of an e-mail service to the J2EE platform.

Figure 20.1 illustrates the JavaMail architecture and the interaction of the two main APIs.

Figure 20.1. The JavaMail architecture.


As you learned from Day 15, “Understanding J2EE Architecture,” JavaMail can be used from within components of either the Web tier or the EJB tier. It can't be used from J2EE client applications or applets.

Comparing JavaMail and JMS

JavaMail and JMS have something in common: both Both are used for delivering asynchronous messaging in J2EE applications. However, there are many differences between the two APIs, including the purpose and the implementations.

Java JMS is used as a unified API to access a MOM (Message-Oriented Middleware) provider, whereas JavaMail is used to access an e-mail system provider. JMS is designed to produce and consume messages by applications, and not by users, as is the case in JavaMail.

Both JMS and JavaMail are used to design loosely coupled applications, which is different from those tightly coupled applications built using RMI/IIOP. In asynchronous communication, users or components send messages, and they do not have to wait for an immediate response. You've covered JMS in detail on Day 13, “Understanding JMS and Message-Driven Beans.”

JavaBeans Activation Framework

The JavaBeans Activation Framework (JAF) API integrates support for MIME (Multipurpose Internet Mail Extensions) data types into the Java platform. This means that you are not limited to using only plain text e-mail messages. You can use many different content types, such as HTML, images, sound, and video. JavaBeans components can be specified for particular MIME data operations, such as viewing or editing the data. The JAF API also provides a mechanism to map filename extensions to MIME types, which is useful in sending messages with attachments.

The JAF API is used by the JavaMail API to handle the data included in e-mail messages. Typical applications will not need to use the JAF API directly, although applications making sophisticated use of e-mail might need it. Later today, you'll learn how to use JAF in conjunction with JavaMail to process HTML e-mail messages and messages with attachments.

Reviewing Basic Protocols

The JavaMail API is designed to use the most common protocols for exchanging e-mail messages. Each of the following subsections briefly discusses one of these protocols. A good understanding of the basics of these protocols will help you grasp how to use the JavaMail API. Although the API is designed to be protocol-independent, you can't overcome the limitations of the underlying protocols. Certain protocols support more capabilities than others.

Simple Mail Transfer Protocol

SMTP is the mechanism for delivery of e-mail. This protocol is used for sending outgoing messages from your applications. Your JavaMail-compliant client will communicate with the SMTP server of your company or a particular ISP to deliver e-mail messages to the specified destinations. JavaMail uses the term transport for the service of sending an e-mail message. The SMTP server relays the message to the SMTP server at the destination, and the recipients retrieve the messages through the use of POP, IMAP, or other protocols, as described in the next two sections.

Post Office Protocol

Also known as POP3, this protocol is used to store and retrieve mail to and from an SMTP server. Similar in concept to the U.S Post Office, it defines support for a single mailbox for each user. JavaMail applications connect to a mailbox to retrieve, read, and delete e-mail messages using POP. Other functionality, such as counting unread messages, can also be accomplished from the application. With POP3, the server provides a folder that stores messages as they arrive. When a client connects to a POP3 server, it specifies the folder from which it retrieves the messages and transfers them to a message store on the client.

Internet Message Access Protocol

IMAP (currently in version 4 and called IMAP4) is a more advanced protocol than POP for storing and retrieving e-mail messages. It can access messages from more than one computer, which has become extremely important as reliance on e-mail messaging and use of multiple computers have increased. Additionally, JavaMail applications can take advantage of the fact that users can have multiple folders on the server and multiple users can share those folders. With IMAP, message folders are stored on the mail server, including folders that contain incoming messages and folders that contain archived messages.

Multipurpose Internet Mail Extensions

MIME is not a mail transfer protocol, per se. Instead, it defines the content of the messages to be handled, such as the format and the attachments. As a user of the JavaMail API, you usually don't need to worry about these formats. However, these formats do exist and are used by your programs. To handle non-plain text mail content, the JavaBeans Activation Framework is required, as we discussed earlier today.

NNTP and Others

The JavaMail architecture provides support for new mechanisms and protocols to be added to the existing family of protocols. This is due to the separation of the JavaMail API into application-specific and provider-specific APIs. Some of the newly added protocols are the S/MIME (Secure Multipurpose Internet Mail Extensions) protocol, NNTP (which is used for newsgroups), and more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.176.166