1、JavaMail FrameWork for developing internet-based e-mail client applications 原文: E-mail functionality is an important system requirement in areas such as e-commerce, customer care, work-flow management and unified messaging. In addition, some application architectures may need to support not only sta
2、ndard mail protocols but also proprietary ones. If youre charged with the task of developing an e-mail client application in Java, you have a number of architectural and design options: building your own services layer to support multiple mail protocols, purchasing third-party components that provid
3、e e-mail features or using JavaSofts JavaMail framework. This article focuses on the JavaMail framework for developing Internet-based e-mail client applications. The JavaMail API shields application developers from implementation details of specific mail protocols by providing a layer of abstraction
4、 designed to support current e-mail standards and any future enhancements. JavaMail increases a developers productivity by allowing the developer to focus on the business logic of an application rather than on mail protocol implementation. It provides a platform and protocol-independent means of add
5、ing e-mail client features to your applications. JavaMail version 1.1 is a standard Java extension and requires JDK/JRE 1.1.x or higher. Overview of Internet Mail Protocols Before getting into the details of JavaMail, a quick overview of some messaging terms and Internet e-mail protocols (IMAP, POP,
6、 SMTP) is in order. A message is described in terms of a header and content. A message header comprises information such as sender (from), recipient (to), and message ID (a unique message identifier). A messages content is the actual message body and can comprise multiple parts in the form of text a
7、nd attachments, as shown in Figure Figure 1: An e-mail client is used to transfer messages to and from an e-mail server, which is responsible for sending and receiving e-mail messages across the Internet. These servers store a users messages either permanently or until retrieved by an e-mail client.
8、 To develop an e-mail client you need to deal with protocols for sending and receiving messages. As shown in Figure 2, the common protocol for sending Internet e-mail messages is SMTP (Simple Mail Transfer Protocol). The original SMTP specification limited messages to a certain line length and allow
9、ed only 7-bit ASCII characters. The MIME (Multipurpose Internet Mail Extensions) specification builds on SMTP by removing the maximum line length for messages and allowing new types of content (e.g., images, binary files) to be included in e-mail messages. The MIME specification overcomes these limi
10、tations by defining additional fields in a message header to describe new types of content and message structure. MIME defines the content-type header to specify the type and subtype of message content. For example, a message with an HTML attachment would have a content-type header set to text/html.
11、 SMTP and MIME are typically used together to send Internet e-mail messages. Figure 2: Two types of protocols are used to retrieve messages from Internet mail servers: POP3 (Post Office Protocol 3) and IMAP4 (Internet Message Access Protocol 4). Although POP3 is more widely used than IMAP4, the latt
12、er has a number of advantages. First, IMAP supports multiple folders on a remote mail server whereas POP3 supports only the Inbox folder. Second, IMAP supports message status flags (e.g., indicates whether a message has been previously seen); POP3 doesnt. These types of protocol features are importa
13、nt considerations in designing your application. JavaMail provides implementations of SMTP, IMAP4 and POP3. For more information on these Internet mail protocols, you can consult the pertinent RFCs. JavaMail Architecture Now that you have a basic understanding of Internet mail terms, we can begin to
14、 discuss the JavaMail architecture. As shown in Figure 3, the architecture can be described in terms of three main layers. JavaMails layered architecture allows clients to use the JavaMail API with different message access protocols (POP3, IMAP4) and message transport protocols (SMTP). Figure 3: The
15、 top layer is the application layer that uses the JavaMail API. The second layer is the JavaMail API that defines a set of abstract classes and interfaces for supporting e-mail client functionality. This is the layer that frees a developer from having to deal with protocol-specific complexities. Jav
16、aMail provides concrete subclasses of these abstract classes for Internet mail. The JavaMail API layer depends on concrete implementations of protocols. The implementation layer forms the third layer of the JavaMail architecture. Since JavaMail is protocol independent, its up to service providers to
17、 implement specific message access and message transfer protocols. Service provider implementations play a role similar to that of JDBC drivers. A provider registry allows service providers to register their protocol implementations to be used by JavaMail APIs. The JavaMail Web site has more informa
18、tion on third-party service providers. JavaBeans Activation Framework JavaMail interacts with message content through an intermediate layer called the JavaBeans Activation Framework (JAF), part of the Glasgow specification (a future release of the JavaBeans component model specification). In terms o
19、f dealing with e-mail messages, JAF provides a uniform way of determining the type of a messages content and encapsulating access to it. The JAF is implemented as a standard Java extension. Sun provides a royalty-free implementation of JAF that requires JDK 1.1.x or higher. JAF is used to get and se
20、t a messages text and attachments. JavaMail provides convenient methods to interact with JAF. For example, MimeMessages setText() method can be used to set a string as a messages content with a MIME type of text/plain. Another example is MimeMessages getContent() method, which returns a messages con
21、tent as a Java object by invoking methods on JAFs DataHandler class. JAF can also be used to view e-mail attachments such as a text file (.txt) or an image (.gif) by instantiating a JavaBean that supports a particular command (e.g., view) on a specific type of message content. As shown below, the JA
22、F DataSource object, which encapsulates the attachment, is used to create a DataHandler object. The DataHandler object uses a CommandInfo object to retrieve the pertinent JavaBean that can be used to perform a specific operation on an attachment. The JavaBean component can then be added to a frame,
23、as shown in the code snippet below. Currently, reference implementations of JAF-aware JavaBeans are available to view text, GIF and JPEG files. CommandInfo, DataSource and DataHandler are all JAF classes. / file name represents the attachment FileDataSource attachFds = new FileDataSource(attachmentF
24、ilename); DataHandler dh = new DataHandler(attachFds); CommandInfo viewCi = dh.getCommand(view); Frame attachmentWindow = new Frame(View Attachment); / add the bean to view the attachment to the main window attachmentWindow.add(Component)dh.getBean(viewCi); Examples Using JavaMail You now have a basic overview of the JavaMail architecture and JAF so we can discuss the main JavaMail classes and methods needed to support an e-mail client. Table 1 describes some fundamental JavaMail classes.