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, doing this for more than the most trivial of applications 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 1.1 and later that provides a class library for fairly sophisticated email clients. It’s a required component of the Java 2 Platform, Enterprise Edition ( J2EE). The JavaMail API can be implemented in 100% Pure JavaTM using sockets and streams, and indeed Sun’s reference implementation is so implemented. Programs use the JavaMail API to communicate with SMTP 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 POP3, Lotus Notes, or MH. You can even get providers that add support for NNTP, the protocol used to transport ...

Get Java Network Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.