Chapter 17. Java and Electronic Mail


Sending and receiving email from a program is easy with Java. If you are writing an applet, you can simply get the browser to compose and send it for you. Otherwise, you can use the JavaMail Extension (package javax.mail) to both send and read mail. JavaMail provides three general categories of classes: Messages, Transports, and Stores. A Message, of course, represents one email message. A Transport is a way of sending a Message from your application into the network or Internet. A Store represents stored email messages and can be used to retrieve them as Message objects. Thus, a Transport is for sending email and a Store is for reading it. One other class, Session, is used to obtain references to the appropriate Store and/or Transport objects that you need to use.

The JavaMail package is included in JavaEE and can be downloaded for JavaSE use. It’s worth it: for the cost of a few minutes’ downloading time, you get the ability to send and receive electronic mail over a variety of network protocols. The JavaMail project is hosted on; the project’s page is at You can download a JAR file containing al the standard protocol providers (SMTP, IMAP, POP3),, or an API JAR just to compile against, or download the basic API and download the protocols individually. There are Maven artifacts in Maven Central as well; see|ga|1|com.sun.mail. There is a JavaMail ...

Get Java Cookbook, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.