JavaMail API Core Classes

The JavaMail API consists of some interfaces and classes used to send, read, and delete e-mail messages. Though there are many packages in the JavaMail API, will cover the main two packages that are used in Java Mail API frequently: javax.mail and javax.mail.internet package. These packages contain all the JavaMail core classes.

JavaMail API Core Classes

There are two packages that are used in Java Mail API: javax.mail and javax.mail.internet package. These packages contains many classes for Java Mail API. They are:

  • javax.mail.Session class
  • javax.mail.Message class
  • javax.mail.internet.MimeMessage class
  • javax.mail.Address class
  • javax.mail.internet.InternetAddress class
  • javax.mail.Authenticator class
  • javax.mail.PasswordAuthentication class
  • javax.mail.Transport class
  • javax.mail.Store class
  • javax.mail.Folder class etc.

Session Class-

The Session class is the primary class of the JavaMail API and it is not subclassed. The Session object acts as the connection factory for the JavaMail API, which handles both configuration setting and authentication.

Session object can be created in the following ways:

1. By looking up the administered object stored in the JNDI service

InitialContext ctx = new InitialContext();
Session session = (Session) ctx.lookup("usersMailSession");

usersMailSession is the JNDI name object used as the administered object for the Session object. usersMailSession can be created and configured with the required parameters as name/value pairs, including information such as the mail server hostname, the user account sending the mail, and the protocols supported by the Session object.

2. Another method of creating the Session object is based on the programmatic approach in which you can use a java.util.Properties object to override some of the default information, such as the mail server name, username, password, and other information that can be shared across your entire application.

The constructor for Session class is private. Hence the Session class provides two methods (listed below) which get the Session object.

3. getDefaultInstance(): There are two methods to get the session object by using the getDefaultInstance() method. It returns the default session.

public static Session getDefaultInstance(Properties props)
public static Session getDefaultInstance(Properties props,Authenticator auth)

4. getInstance(): There are two methods to get the session object by using the getInstance() method. It returns the new session.

public static Session getInstance(Properties props)
public static Session getInstance(Properties props,Authenticator auth)

Message Class

With Session object created we now move on to creating a message that will be sent. The message type will be javax.mail.Message.

1. Message is an abstract class. Hence its subclass javax.mail.internet.MimeMessage class is mostly used.

2. To create the message, you need to pass session object in MimeMessage class constructor.
For example:

MimeMessage message=new MimeMessage(session);

3. Once the message object is created we need to store information in it. Message class implements the javax.mail.Part interface while javax.mail.internet. MimeMessage implements javax.mail.internet.MimePart. You can either use message.setContent() or mimeMessage.setText() to store the content.

4. Commonly used methods of MimeMessage class are

  • public void setFrom(Address address) used to set the from header field.
  • public void addRecipients(Message.RecipientType type, String addresses) used to add the given address to the recipient type.
  • public void setSubject(String subject) used to set the subject header field.
  • public void setText(String textmessage) used to set the text as the message content using text/plain MIME type.

Address Class

Now that we have a Session and Message (with content stored in it) objects, we need to address the letter by using Address object.

Address is an abstract class. Hence its subclass javax.mail.internet.InternetAddress class is mostly used.

1. Address can be created by just passing email address:

Address address = new InternetAddress("admin@gmail.com"); 

2. Another way of creating Address is by passing name alogwith the email address:

Address address = new InternetAddress("admin@gmail.com", admin); 

3. You can also set the To, From, CC, BCC fields as below

message.setFrom(address)
message.addRecipient(type, address)

4. Three predefined address types are objects with one of these values:

Message.RecipientType.TO
Message.RecipientType.CC
Message.RecipientType.BCC

Authenticator Class

The class Authenticator represents an object that knows how to obtain authentication for a network connection. Usually, it will do this by prompting the user for information.

Authenticator is an abstract class. You create a subclass PasswordAuthentication, passing a username and password to its constructor.

You must register the Authenticator with the Session when you create session object.

Following is an example of Authenticator use:

Properties props = new Properties();
//Override props with any customized data
PasswordAuthentication auth = new PasswordAuthentication("admin", "root")
Session session = Session.getDefaultInstance(props, auth);

Transport Class

Transport class is used as a message transport mechanism. This class normally uses the SMTP protocol to send a message.

1. It is an abstract class.

2. You can use the default version of the class by just calling the static send() method:

Transport.send(message);

3. The other way to send message is by getting a specific instance from the session for your protocol, pass along the username and password (blank if unnecessary), send the message, and close the connection:

message.saveChanges(); // implicit with send()
//Get transport for session
Transport transport = session.getTransport("smtp");
//Connect
transport.connect(host, username, password);
//repeat if necessary
transport.sendMessage(message, message.getAllRecipients());
//Done, close the connection
transport.close();

Store Class

An abstract class that models a message store and its access protocol, for storing and retrieving messages. Subclasses provide actual implementations. Store extends the Service class, which provides many common methods for naming stores, connecting to stores, and listening to connection events.

Clients gain access to a Message Store by obtaining a Store object that implements the database access protocol. Most message stores require the user to be authenticated before they allow access. The connect method performs that authentication.

Store store = session.getStore("pop3");
store.connect(host, username, password);

Folder Class

Folder is an abstract class that represents a folder for mail messages. Subclasses implement protocol specific Folders. Folders can contain subfolders as well as messages, thus providing a hierarchical structure.

After connecting to the Store, you can then get a Folder, which must be opened before you can read messages from it.

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();

The getFolder(String name) method for a Folder object returns the named subfolder. Close the both the Store and Folder connection once reading mail is done.

<<Previous <<   || Index ||   >>Next >>
Previous
Next