Chapter 19. The JavaMail API
Email was the
Internet's first killer app and still generates more
Internet traffic than any protocol except HTTP. One of the most
frequently asked questions about Java is how to send email from a
Java applet or application. While it's certainly
possible to write a Java program that uses sockets to communicate
with mail servers, this requires detailed knowledge of some fairly
complicated protocols, such as SMTP, POP, and IMAP. Just as the
URL class makes interacting with HTTP servers a
lot simpler than it would be with raw sockets, so too can a class
library dedicated to handling email make writing email clients a lot
simpler.The JavaMail API is a standard extension to Java that provides a
class library for email clients. It's a required
component of the Java 2 Platform, Enterprise Edition (J2EE). The
JavaMail API can be implemented in 100% Pure Java™
using sockets and streams, and indeed Sun's
reference implementation is so implemented. Programs use the JavaMail
API to communicate with SMTP, POP, and IMAP servers to send and
receive email. By taking advantage of this API, you can avoid
focusing on the low-level protocol details and focus instead on what
you want to say with the message. Additional providers can add
support for other mail systems such as Hotmail or MH. You can even
install providers that add support for NNTP, the protocol used to
transport Usenet news.There's no limit to the uses Java programs have for
the JavaMail API. Most obviously, you can write standard email
clients such as Eudora. Or it can be used for email-intensive
applications such as mailing list managers, like listproc. But the
JavaMail API is also useful as a part of larger applications that
simply need to send or receive a little email. For instance, a
server-monitoring application such as Whistle Blower can periodically load
pages from a web server running on a different host and email the
webmaster if the web server has crashed. An applet can use email to
send data to any process or person on the Internet that has an email
address, in essence using the web server's SMTP
server as a simple proxy to bypass the usual security restrictions
about whom an applet is allowed to talk to. In reverse, an applet can
talk to an IMAP server on the applet host to receive data from many
hosts around the Net. A newsreader could be implemented as a custom
service provider that treats NNTP as just one more means of
exchanging messages. And that's just the beginning
of the sort of programs the JavaMail API makes it very
straightforward to write.