JavaMail API - SPLessons

javaMail API Core Classes

Chapter 3

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

javaMail API Core Classes

javaMail API Core Classes

shape Introduction

JavaMail API is a platform, protocol independent framework for to send or receive mail. JavaMail API also provides core classes for to define objects and used that objects to maintain a mail system.Generally the javaMail API contains mainly the javax.mail and javax.mail.internet packages, these two packages contain JavaMail API Core Classes. By making use of these JavaMail API Core Classes sending and receiving the emails. Following are the classes.

These are the core class of the javaMail and explained below in detail.

Session class

shape Description

Session class is primary class to javaMail API. The object from session class i.e. session object is used as connection factory for javaMail API and take care about authentication and configuration settings. The session class constructor is private.

The administered object to session object is used from UserMailSession(JNDI name object). After creating UserMailSession configuration is done with required parameters with mail server called host name and with the protocols which are supported by session object.

By programmatic method can create the session object for that have to use java.Util.Properties to override some information regarding server name and other which is is useful for java application.

getDefaultInstance() and getInstance() are the two methods used in session class to get the session object.

Message Class

shape Description

The message class is one of the abstract class. For creating the message, have to pass the session object to MineMessage class constructor subclass to Message class.

MineMessage message=new MineMessage(session);

There are some methods used in MineMessage, they are

Address class

shape Description

The address class is one of the abstract class. Creating the address by passing the email id or address

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

Where the internet address class is subclass to address class. And also possible to create address by sending name along with email id.

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

To set the fields while sending an email have to follow the following steps

  • message.setFrom(Address)
  • message.addRecipient(type, address)
  • message.RecipientType.TO
  • message.RecipientType.CC
  • message.RecipientType.BCC

Authenticator class

shape Description

The authenticator class is responsible for the network connection authentication and it is an abstract class. When creating session object the authenticator have to register with it.

PasswordAuthentication aut=new PasswordAuthentication("splesson", "password");

Here the passwordAuthentication is subclass to Authentication class.

Transport class 

shape Description

The mechanism of this class is to transfer the message using SMTP protocol. Transport class is an abstract class. static send() method is called to get the default version of the class.

Transport.send(message);

There is another way to send or transfer message through the session object along with username and password and close connection.


Store class

shape Description

Store class is an abstract class and this class represents a message store for storing and retrieving messages. The service class which is subclass to store class implements methods like connection and naming to stores. The authenticated users are allowed to access the message store.
Store s = session.getStore("pop3");
Store.connect(host, username, password);

Folder class

shape Description

Folder class is an abstract class to specify the folder for mail messages. To get the folder only after store connection is possible. After completion of reading the message close the store and connection.

shape Conceptual
figure


From the above figure, showing difference between folder and store. The store is for storing messages and here the store is divided into folders and the primary folder represents the Inbox and remaining are sub-folders.

Summary

shape Key Points

  • The javaMail API contains mainly the javax.mail and javax.mail.internet packages.
  • These two packages contain JavaMail API Core Classes.
  • By making use of these JavaMail API Core Classes sending and receiving the emails.
  • To get session object getInstance() or getDefaultInstance() method is used.
  • Creating address by passing email address from Address class.
  • Transfer class is the mechanism to transfer messages.
  • Store class is for storing the messages and retrieving it.
  • Folder class is for to get folder only after store connection is done.