JavaMail API
The JavaMail API is a standard part of the Java 2 Platform, Enterprise Edition (J2EE). As its name suggests, the JavaMail API provides support for sending and receiving mail messages. It is useful for successful update of data or sending automated information from Java Application.
The JavaMail API involves four classes:
Session
For sending and receiving messages, we must first connect to a mail server. JavaMail API provides a framework for making that connection. Session represents the basic mail session with the mail server. You get the session with either getDefaultInstance() or getInstance() methods of the Session class.The getDefaultInstance(Properties props), returns default Session object. If a default has not yet been setup, a new Session object is created and installed as the default. Properties object is used only if a new Session object is created.
Message
After we have the Session object, we can create a Message from the Session. Message is an abstract class, so we create an instance of its subclass, the MimeMessage class.This class has different methods to set the content of the message like from, to, subject and the body.
setFrom(): method used to set the From address
addRecipient(): method used to set the To, CC, and BCC addresses
setSubject(): method used to set the Subject
setText(): message used to set the actual content
The setFrom() method simply passes the from-address. The addRecipient() method involves a little more work than using setFrom(). With addRecipient(), you need to specify whether the Address is for the To field, the CC field, or the BCC (Blind carbon copy) field. This is done with the help of the RecipientType class:
Message.RecipientType.TO
Message.RecipientType.CC
Message.RecipientType.BCC
The setSubject() and setText() methods just accept the text content of their respective message pieces.
Address
In order to set the From or To fields of the message, you need an Address, which you create with the InternetAddress class. You create an Address from a string, and pass the Address to the appropriate method.Transport
After framing the message, send it with the Transport class. This uses the SMTP server, specified in the properties of the session.Here is how to use the JavaMail API to send a Mail. The JavaMail API requires certain external JAR files to be included, which can be downloaded from
http://java.sun.com/products/javamail/downloads/index.html
(Note: After downloading, extract the contents of the zip file. The JAR files are available at location /javamail-1.4.2/ javamail-1.4.2/lib)
Also to test the program, we need to have SMTP and port numbers. For testing purpose, we are using Gmail SMTP and port number. Check from the following URL: http://techblissonline.com/gmail-smtp/
(Note: Gmail SMTP requires authentication. Transport Layer Security (TLS) sometimes called SSL is used to protect username/password when sent across the network for the mail protocols).
JavaMail API without attachment
JavaMail API with attachment(s)
If we want to attach files to the email message, we need to know a few more classes
Multipart
Message with attachment(s) need to be built as parts quite literally. The content of your Message will consist of multiple parts within a Multipart object.BodyPart
The first part is a BodyPart, that contains the message content or the actual content and the second part is a BodyPart, that contains the attachment(s).For part one, create a BodyPart and set the text or the actual message. Then, add the BodyPart to the Multipart object. For part two, again create a BodyPart object, but this time, create a DataSource for the file. Attach part two in the same way as part one and as a final step before sending, attach the Multipart to the Message
DataSource
attachment is specified as a DataSource.DataHandler
DataHandler object is used to attach the DataSource to the message.Here is how to use the JavaMail API to send a Mail with attachment(s). When we have files attached to email, we will also need the JavaBeans Activation Framework or JAF (javax.activation) which can be downloaded from http://java.sun.com/javase/technologies/desktop/javabeans/jaf/downloads/index.html
(Note: After downloading, extract the contents of the zip file. The JAR file (activation.jar) available at location /jaf-1_1_1/ jaf-1.1.1)
We start the same way as we do for message without an attachment. However here you need to create the Multipart content.

