JavaMail API
Elliotte Rusty Harold
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Download from Wow! eBook
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
Preface
Elliotte Rusty Harold
elharo@ibiblio.org
Email was the Internets first killer app. One of the most frequently asked questions about Java is how to send email from a Java applet or application. While its 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. Its a required component of the Java Platform, Enterprise Edition. The JavaMail API can be implemented in pure Java using sockets and streams, and indeed Oracles 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. Custom providers can support the proprietary features of mail systems such as Gmail and Microsoft Exchange. You can even install providers that add support for NNTP, the protocol used to transport Usenet news.
Theres no limit to the uses Java programs have for the JavaMail API. Most obviously, you can write standard email clients such as Thunderbird. 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 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 servers 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 thats just the beginning of the sort of programs the JavaMail API makes it very straightforward to write.
Who You Are
This book assumes you are comfortable with the Java language and programming environment, in addition to object-oriented programming in general. This book does not attempt to be a basic language tutorial. You should be thoroughly familiar with the syntax of Java. You should have written at least simple applications.
Java Versions
This book is written with the assumption that you are coding with at least Java 5.0. In general, I use Java 5 features like generics and the enhanced for loop freely without further explanation. With respect to the JavaMail API, the distinction between Java 5 and Java 6 is not large. Most examples look identical in the two versions.
Java 7 is a bit more of a stretch. I have not shied away from using features introduced in Java 7 where they seemed especially useful or convenientfor instance, try-with-resources and multicatch are both very helpful when fitting examples into the limited space available in a printed bookbut I have been careful to point out my use of such features.
As to JavaMail itself, this book covers JavaMail 1.5 which requires Java 5 or later. If for some reason youre stuck on Java 1.4, JavaMail 1.4.1 is still available and will work with your JDK. Whichever version you use, the JavaMail API is not bundled with the base JDK or JRE, even in Java 8. You can download it separately from https://java.net/projects/javamail/.
Conventions Used in This Book
Body text is Times Roman, normal, like youre reading now.
A monospaced typewriter font
is used for:
- Code examples and fragments
- Anything that might appear in a Java program, including keywords, operators, data types, method names, variable names, class names, and interface names
- Program output
- Tags that might appear in an HTML document
A bold monospaced font
is used for:
- Command lines and options that should be typed verbatim on the screen
An italicized font is used for:
- New terms where they are defined
- Pathnames, filenames, and program names (however, if the program name is also the name of a Java class, it is given in a monospaced font, like other class names)
- Host and domain names ( java.oreilly.com )
- URLs ( http://www.cafeaulait.org/slides/ )
- Titles of other chapters and books ( JavaI/O )
Significant code fragments and complete programs are generally placed into a separate paragraph, like this:
Message msg = new MimeMessage(session);Address elliotte = new InternetAddress("elharo@ibiblio.org");msg.setFrom(elliotte);
When code is presented as fragments rather than complete programs, the existence of the appropriate import
statements should be inferred. For example, in the above code fragment you may assume that javax.mail.Address
and javax.mail.Message
were imported.
Tip
Indicates a tip, suggestion, or general note.
Warning
Indicates a warning or caution.
Request for Comments
I enjoy hearing from readers, whether with general comments about this book, specific corrections, other topics you would like to see covered, or just war stories about your own network programming travails. You can reach me by sending email to . Please realize, however, that I receive several hundred pieces of email a day and cannot personally respond to each one. For the best chances of getting a personal response, please identify yourself as a reader of this book. If you have a question about a particular program that isnt working as you expect, try to reduce it to the simplest case that reproduces the bug, preferably a single class, and paste the text of the entire program into the body of your email. Unsolicited attachments will be deleted unopened. And please, please send the message from the account you want me to reply to and make sure that your Reply-to address is properly set! Theres nothing quite so frustrating as spending an hour or more carefully researching the answer to an interesting question and composing a detailed response, only to have it bounce because my correspondent was sending from a public terminal and neglected to set the browser preferences to include their actual email address.