javaMail API Core Classes
javaMail API Core Classes
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.
- 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.
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.
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
- 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.
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
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.
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.
Transport trns= session.getTransport(“smtp”);
Transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
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);
Description
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”);
folder.open(Folder.READ_ONLY);
Messages message = folder.getMessages();
After completion of reading the message close the store and connection.
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.