Send Receive Mails in Java with JavaMail
The JavaMail API (http://java.sun.com/products/javamail/) is a messaging framework intended to build platform-independent applications that use e-mail messaging. It is included in the J2EE platform and is available as an optional package in J2SE. The API's main purpose is not for transporting, delivering, and forwarding messages, but rather providing protocol-independent access to mail infrastructure for sending and receiving messages.
The JavaMail API is divided into two parts:
» The main part of the API is focused on sending and receiving messages (independent of the protocol).
» The second part implements protocol-specific languages, such as SMTP, POP, IMAP, and NNTP. With the JavaMail API, in order to communicate with a server, you need a provider for a protocol. (Sun provides a sufficient set of providers for free - at least SMTP, POP, and IMAP, which is more than enough for sending and receiving standard e-mail messages over standard Internet infrastructure.)
All versions of the JavaMail API require the JavaBeans Activation Framework, which adds support for typing arbitrary blocks of data and handling it accordingly. JavaMail uses Multipurpose Internet Mail Extensions (MIME) implementation from that framework. The MIME implementation defines the content of what is transferred in an e-mail: the format of messages, attachments, and so on. In order to use JavaMail, an application must include classpathmail.jar and activation.jar to operate properly. If you use J2EE, there is nothing special you have to do to use the basic JavaMail API - it comes with J2EE classes.
In 2006, JavaMail became open source, and the source code for the JavaMail API Reference Implementation is now available under the open source license as part of the project called GlassFish (https://glassfish.dev.java.net/).
Here is an overview of some of the most important classes used in JavaMail applications:
» javax.mail.Session: This class is used to control access to the implementations of the other mail classes that represent the services offered by the mail system, for instance javax.mail.Store. (Be careful not to confuse this class with javax.servlet.http.HttpSession.)
» javax.mail.Transport: This class is used for sending mail messages via a specific protocol such as SMTP, as implemented by the service provider.
» javax.mail.Store: This class is implemented by the service provider. It aims to allow access to read, write, monitor, and search activities for a particular mail protocol such as POP3 or IMAP4. A reference to the javax.mail.Folder class is obtained via this class.
» javax.mail.Folder: This class gives a hierarchical view of javax.mail.Message objects and provides access to specific messages for read, delete, and reply actions.
» javax.mail.internet.MimeMessage: This class models the actual mail message. It holds very little information and data about the message when it is first instantiated; as successive methods retrieve more data about the message, this class is used to store that data.
» javax.mail.internet.InternetAddress: This class models a RFC822 (Internet standard) e-mail address, i.e., an address of the form firstname.lastname@example.org. If an incorrect address format is encountered, an error occurs and an AddressException is thrown within the Java method processing the e-mail address.
Snippet A contains sample code that sends a test message. In this example, you should replace the placeholder e-mail addresses with real addresses and replace smtp.go4expert.com with the address of a real SMTP server that is used by your Internet provider or company.
Finally, the message is ready to be sent. In JavaMail, the Transport object handles sending messages. Since we only have one, the static send method is used. This method uses the SMTP server specified as the "mail.smtp.host" address.
The JavaMail API has many advanced capabilities. As an example, you can add any mail headers to the message, set up multiple recipients, create blind copies of a message (what BCC is intended for), recode the message text into different charset encodings, and so on. For example, Listing B shows how to add multiple recipients.
The ways in which you can use the JavaMail API are beyond the scope of this article. For a full description, refer to JSR 919, which is the latest JavaMail 1.4 specification. The Sun JavaMail API is the full reference implementation of this specification, so just install it and then take the rich features out for a test drive.
|All times are GMT +5.5. The time now is 08:58.|