The Javamail API is an open source implementation that enables users to make use of a full fledged implementation to set up an email system based entirely on Java.
The API allows programmers to create entire “email clients” or application suites like hotmail which could be used to send and receive email, multimedia and the likes.
The Javamail API is highly robust; The Application that is designed can not only be something in the lines of a desktop app. But it can also be a small web-based, user driven SMTP client.
The API Supports :
- SMTP
- POP3
- IMAP
Installing the API
The API comes built-in with higher-end versions of Java 2. It has been built into the Java 2 Enterprise Edition and comes as an optional add-on for the standard edition.
For the Java 2 Std Edition, one needs to download the following :
Javamail :
http://www.oracle.com/technetwork/java/javamail/index-138643.html
JavaBeans Activation Framework : Needed for MIME multimedia extensions.
http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html
Once the API’s classes are installed, the following jar files need to be added in the classpath :
- Mailapi.jar – The jar contains the core API classes.
- Pop3.jar and smtp.jar – Provides the POP3/SMTP implementations for the API
- Activation.jar – Contains the MIME extensions that can be coupled alongside the API.
The Javamail Framework – Functions
The standard process framework involves the use of the following core classes :
A Message is created using the Part Interface and the Message Class contains
A group of header attributes.
A Content-Type field.
Block of data that make up the message.
A Session is created using the Session Class to manage the entire setup.
A session uses the Authenticator object to authenticate the user.
It Control access to the store and transport objects.
Send the Message to the Recipient.(at the sender’s end)
Retrieve a message from the stored folder(at the receiver’s end)
Core Classes Of the API :
- Session
- Message
- Address
- Authenticator
- Transport
- Store
- Folder
The Session Class :
Global and mail-related features that relate to communication between the email client and the network is defined using the Session Class.
Provides a default authenticated object whenever invoked.
Final Class – Cannot be inherited. Has the ability however to control and act as a factory for the service provider implementations.(i.e. user developed versions of the email client.)
The Store Class :
This class’s implementation allows users to read, write, browse through and search archives for messages relating to a particular mail protocol. The Session object acts as a factory for the Store and the Transport objects respectively.
The Store class has direct access to the Folder class.
The Transport Class :
Provides transport specific information. Acts as an agent that routes messages to its destination addresses. Provides features that enable mails to be sent to multiple users.
The Folder Class :
This class provides a hierarchical structure to store email messages and also provides access to emails by instantiating the Message Class.
Specific to an application client – i.e. needs to be implemented by the developer as
per the application’s needs and specifications.
The Address Class :
Represents an email address.
The Authenticator Class :
Provides authentication specifics for the client application.
The Message Class :
Developer needs to implement this class according to the specifications of the Email Client.
Contains the details of the actual mail message :
- The Subject line
- Sender/recipient address.
- Content.
- Sent-date.
- Content Type