

























































































Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!


























































































JavaMail API by Elliotte Rusty Harold
Copyright © 2013 Elliotte Rusty Harold. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( http://my.safaribooksonline.com ). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected].
Editor: Meghan Blanchette Production Editor: Melanie Yarbrough Proofreader: Nicole Shelby
Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Rebecca Demarest
July 2013: First Edition
Revision History for the First Edition:
2013-07-22: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449367244 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Java Mail API , the cover image of a little pied cormorant, and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
ISBN: 978-1-449-36724-
[LSI]
Preface
Email was the Internet’s first killer app. 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 Platform, Enterprise Edition. The JavaMail API can be implemented in pure Java using sockets and streams, and indeed Oracle’s reference implementation is so implemented. Programs use the JavaMail API to com‐ municate 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.
There’s 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 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
v
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 straight‐ forward to write.
Who You Are
This book assumes you are comfortable with the Java language and programming en‐ vironment, 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 convenient—for instance, try-with- resources and multicatch are both very helpful when fitting examples into the limited space available in a printed book—but 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 you’re 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 you’re reading now.
A monospaced typewriter font is used for:
A bold monospaced font is used for:
vi | Preface
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! There’s 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.
I also adhere to the old saying “If you like this book, tell your friends. If you don’t like it, tell me.” I’m especially interested in hearing about mistakes. This is my eighth book. I’ve yet to publish a perfect one, but I keep trying. As hard as I and the editors at O’Reilly worked on this book, I’m sure there are mistakes and typographical errors that we missed here somewhere. And I’m sure that at least one of them is a really embarrassing whopper of a problem. If you find a mistake or a typo, please let me know so I can correct it. I’ll post it on the O’Reilly website. Before reporting errors, please check one of those pages to see if I already know about it and have posted a fix. Any errors that are reported will be fixed in future printings.
Using Code Examples
This book is here to help you get your job done. In general, if this book includes code examples, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require per‐ mission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “ JavaMail API by Elliotte Rusty Harold (O’Reilly). Copyright 2013 Elliotte Rusty Harold, 978-1-449-36724-4.”
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at [email protected].
Safari® Books Online
Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s lead‐ ing authors in technology and business.
viii | Preface
Technology professionals, software developers, web designers, and business and crea‐ tive professionals use Safari Books Online as their primary resource for research, prob‐ lem solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organi‐ zations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Pro‐ fessional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technol‐ ogy, and dozens more. For more information about Safari Books Online, please visit us online.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://oreil.ly/javamail-api.
To comment or ask technical questions about this book, send email to bookques [email protected].
For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
Many people were involved in the production of this book.
Edward Wong, Alex Stangl, and Bill Shannon ably reviewed early drafts and substan‐ tially improved the coverage of many topics. Development editor Meghan Blanchette
Preface | ix
CHAPTER 1
Introducing the JavaMail API
The JavaMail API is a fairly high-level representation of the basic components of any email system. The components are represented by abstract classes in the javax.mail package. For instance, the abstract class javax.mail.Message represents an email mes‐ sage. It declares abstract methods to get and set various kinds of envelope information for the message, such as the sender and addressee, the date sent, and the subject. The abstract class javax.mail.Folder represents a message container. It declares abstract methods to retrieve messages from a folder, move messages between folders, and delete messages from a folder.
These classes are all abstract because they don’t make many assumptions about how the email is stored or transferred between machines. For instance, they do not assume that messages are sent using SMTP or that they’re structured as specified in RFC 822. Con‐ crete subclasses of these classes specialize the abstract classes to particular protocols and mail formats. If you want to work with standard Internet email, you might use jav ax.mail.MimeMessage instead of javax.mail.Message, javax.mail.InternetAd dress instead of javax.mail.Address, and com.sun.mail.imap.IMAPStore instead of javax.mail.Store. If you were writing code for a Microsoft Exchange-based system, you’d use different concrete implementation classes but the same abstract base classes.
The JavaMail API roughly follows the abstract factory design pattern. This pattern al‐ lows you to write your code based on the abstract superclasses without worrying too much about the lower-level details. The protocols and formats used and the associated concrete implementation classes are determined mostly by one line of code early in the program that names the protocol. Changing the protocol name goes 90% of the way toward porting your program from one protocol (say, POP) to another (say, IMAP).
Service providers implement particular protocols. A service provider is a group of con‐ crete subclasses of the abstract JavaMail API classes that specialize the general API to a particular protocol and mail format. These subclasses are probably (though not
1
necessarily) organized into one package. Some of these (IMAP, SMTP) are provided with the reference implementation in the undocumented com.sun.mail package. Oth‐ ers (NNTP, Exchange) are available from third parties. And some (POP) are available from both Oracle and third parties. The purpose of the abstract JavaMail API is to shield you from low-level details like this. You don’t write code to access an IMAP server or a POP server; you write code that speaks to the JavaMail API. Then the JavaMail API uses the service provider to speak to the server using its native protocol. This is middleware for email. All you need to do to add a new protocol is install the service provider’s JAR file. Simple, carefully designed programs that use only the core features of the JavaMail API may be able to use the new provider without even being recompiled. Of course, programs that make use of special features of individual protocols may need to be re‐ written.
Since mail arrives from the network at unpredictable times, the JavaMail API relies on an event-based callback mechanism to handle incoming mail. This is exactly the same pattern (even using some of the same classes) found in the Swing and JavaBeans. The javax.mail.event package defines about half a dozen different kinds of mail events, as well as the associated listener interfaces and adapter classes for these events.
While many people still fondly recall the early days of ASCII email and even ASCII pictures, modern email messages contain a bewildering array of multilingual text and multimedia data encoded in formats such as Base64, quoted-printable, BinHex, and uuencode. To handle this, the JavaMail API uses the JavaBeans Activation Framework (JAF) to describe and display this content.
This book covers Version 1.5 of the JavaMail API. The JavaMail API is a standard ex‐ tension to Java, not part of the core JDK or JRE class library, even in Java 8. (It is a standard part of Java Enterprise Edition (JEE)). Consequently, you’ll need to download it separately from Oracle and install it on your system. It’s freely available from Java. It comes as a JAR archive named javax.mail.jar. This file contains the actual .class files that implement the JavaMail API. To compile or run the examples in this book, you’ll need to add this file to your class path, either by adding its path to the CLASSPATH environment variable or by placing javax.mail.jar in your jre/lib/ext directory.
If you’re using Java 5, you will also need to install the JavaBeans Activation Framework. (It’s bundled with the JDK starting in Java 6.) You can download it from Oracle’s web‐ site. This download contains the activation.jar archive, which you’ll also need to place in your class path.
Finally, you may want to add some additional providers. Oracle’s implementation in‐ cludes POP3, SMTP, Gmail, and IMAP providers. However, third parties have written providers for other protocols such as Hotmail, NNTP, Exchange, and more. Table 1- lists some of these.
2 | Chapter 1: Introducing the JavaMail API
CHAPTER 2
Sending Email
Sending messages is the most basic email need of a Java program. While email clients like Thunderbird and mailing list managers like listproc are the only common programs that receive messages, all sorts of programs send messages. For instance, web browsers can submit HTML forms via email. Security scanning tools can run in the background and email their results to the administrator when they’re done. When the Unix cron program detects a misconfigured crontab file, it emails the error to the owner. Books & Writers runs a popular service that tracks the sales rank of authors’ books on Ama‐ zon.com and notifies them periodically via email. A massively parallel computation like the SETI@home project can submit individual results via email. Some multiplayer games like chess can be played across the network by emailing the moves back and forth (though this scheme wouldn’t work for speed chess). And these are just a few of the different kinds of programs that send email. In today’s wired world, by far the simplest way to notify users of an event when they’re not sitting in front of the computer that the program is running on is to send them an email.
The JavaMail API provides everything programs need to send email. To send a message, a program follows these steps:
5
Property Default Value Purpose mail.protocol.class Fully package qualified class name of the provider for the specified protocol mail.transport.protocol first transport provider in the configuration file
Default protocol with which to send messages
mail.alternates Other email addresses for the current user that will not be included when replying to a message mail.replyallcc false When replying to all, put all recipients in the Cc list of the reply message instead of the To field
This Session object is then used to construct a new Message object:
Message msg = new MimeMessage(session);
Here I specify the MimeMessage class in particular since I know I’m sending Internet email. However, this is the one place where I do explicitly choose a format for the email message.
Now that I have a Message object, I need to set up its fields and contents. The From: address and To: address will each be javax.mail.internet.InternetAddress objects. You can provide either an email address alone or an email address and a personal name:
Address bill = new InternetAddress("[email protected]", "Bill Gates"); Address elliotte = new InternetAddress("[email protected]");
The setFrom() method specifies who’s sending the message by setting the From: header. There’s no protection against forgery. It’s quite easy for me to masquerade as Bill Gates at a (presumably) fictitious email address:
msg.setFrom(bill);
The setRecipient() method is slightly more complex. You not only have to specify the address that the message will be sent to, but how that address is used; that is, as a To: field, a Cc: field, or a Bcc: field. These are indicated by three mnemonic constants of the Message.RecipientType class:
Message.RecipientType.TO Message.RecipientType.CC Message.RecipientType.BCC
For example:
msg.setRecipient(Message.RecipientType.TO, elliotte);
The subject is set as a simple string of text. For example:
msg.setSubject("You must comply.");
The body is also set as a single string of text. For example:
Sending Email | 7
msg.setText("Resistance is futile. You will be assimilated!");
Next we specify the transport we want. In 2013, smtps (SMTP over TLS) is the customary choice:
Transport t = session.getTransport("smtps");
Once you have a transport, you connect it to a specified host with a given username and password:
t.connect("smtp.gmail.com", "elharo", "mypassword");
SMTP connections used to be unauthenticated with no username or password. However, spammer abuse made this sort of open relay in‐ feasible in the 21st century. Going forward, even usernames and pass‐ words are less than perfectly secure. I would not be surprised if this example stops working during the lifetime of this book and some more secure oAuth2 or two factor system becomes required to send email.
Next the send() method connects to the mail server and sends the message on its way:
t.sendMessage(msg, msg.getAllRecipients());
The second argument to sendMessage() is an array of addresses to send the message to. Instead of using the addresses in the message itself, you could add additional email addresses. For example:
Address[] addresses = { new InternetAddress("[email protected]"), new InternetAddress("[email protected]"), new InternetAddress("[email protected]"), new InternetAddress("[email protected]") };
transport.sendMessage(msg, addresses);
Finally the close() method shuts down the connection. Transport does not implement AutoCloseable, so even in Java 7 programs normally use the dispose pattern and close the transport in a finally block:
Transport t; try { // work with the transport... } catch (MessagingException ex) { ex.printStackTrace(); } finally { if (t != null ) { try { t.close(); } catch (MessagingException ex) {
8 | Chapter 2: Sending Email