javaMail API Core Classes
javaMail API Core Classes
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
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.
- javax.mail.Session class
- javax.mail.Message class
- javax.mail.internet.InternetAddress class
- javax.mail.Authenticator class
- javax.mail.internet.MineMessage class
- javax.mail.Address class
- javax.mail.PasswordAuthentication class
- javax.mail.Transport class
- javax.mail.Store class
- javax.mail.Folder class
These are the core class of the javaMail and explained below in detail.
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.
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
- public void setFrom(Adderss address)
- public void addReceipients(Messane.RecepientType t, String addresses)
- public void setSubject(String subject)
- public void setText(String text)These are the methods commonly used while sending a email.
The address class is one of the abstract class. Creating the address by passing the email id or address
Address address=new InternetAddress(email@example.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(firstname.lastname@example.org, splessons);
To set the fields while sending an email have to follow the following steps
- message.addRecipient(type, address)
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.
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.
There is another way to send or transfer message through the session object along with username and password and close connection.
Transport trns= session.getTransport(“smtp”);
Transport.connect(host, username, password);
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 is an abstract class to specify the folder for mail messages. To get the folder only after store connection is possible.
Folder f = store.getFolder(“Inbox”);
Messages message = folder.getMessages();
After completion of reading the message close the store and connection.
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.
- 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.