|
JavaMail's Flexible Protocol Support
The JavaMail API and its support of mail and other protocols makes it ideal for creating e-mail tools
by Kevin Jones
Posted January 21, 2004
The JavaMail API is a part of Java 2 Enterprise Edition (J2EE) and as such is the standard method in Java to send, receive, and access e-mail. The API is pluggable, as are other APIs in Java, which means that there is not a fixed set of protocols supported but that different providers can provide their own support for any protocol they care to. Sun provides a reference implementation of the API available from Sun Java Developers site (see Resources). This reference implementation supports the SMTP, POP3, and IMAP mail protocols out of the box (see the sidebar, "Mail Protocols"). Let's take a look at JavaMail in general and using JavaMail to read e-mail from a POP3 server.
The mail protocols are supported by the Sun Reference implementation, but the JavaDocs state the following: "Warning: The APIs unique to this package should be considered experimental. They may be changed in the future in ways that are incompatible with applications using the current APIs." The documentation recommends that users use other providers, and Sun provides a page that lists providers (see Resources). There are both commercial and open source providers available that provide support for mail and other protocols. Notably, there are providers for the MBOX protocol, used widely in Unix (and Linux) systems, and the MH protocol used by Microsoft Outlook, for example. These protocols provide access to client-side storage. That is, after downloading e-mail, a Mail User Agent (MUA), which is just a fancy term for Outlook or Evolution, typically stores the e-mail locally. In the case of Evolution (and other *nix MUAs) this storage is typically in an MBOX file. MBOX files are text files with a well-defined and documented structure, and there are various providers for accessing this storage. (For the MBOX sample I had to dig around. I started by using Knife but couldn't get the MIME message reading working. I ended up using the MBOX provider from Pooka, which just turns out to be a different version of knife [see Resources]).
When I first came across mail APIs about 10 years ago—with first of all CRC and then MAPI in the Microsoft world—I honestly wondered what all the fuss was about. There seemed to be such a limited market for e-mail-based products. Sure, I needed an e-mail client, but there would only ever be a limited number of successful clients. Since then of course e-mail has become the most widely used product on the Internet, and it would be difficult to imagine my life without it. This success means that the market for e-mail-aware applications has grown tremendously, as has the market for e-mail tools such as archival, search, and backup tools. Using an API such as JavaMail greatly simplifies the task of writing these tools. For example, I've recently written Blog software, and one necessary part of a Blog is e-mailing the author when a comment has been added to a blog entry, and using JavaMail makes this a trivial task.
A Matter of Structure
JavaMail is structured very similarly to other J2EE APIs such as JDBC and JNDI. It is a provider-based API and therefore includes many factory classes. The JavaMail API consists of a set of Java interfaces and abstract classes that the client application uses. These classes in turn use an underlying implementation (or implementations) that the application code does not have to be aware of. The underlying implementation classes provide the protocol support.
Back to top
|