Pages

Java Mail API

Java Mail API
Java Mail API

Jakarta Mail (formerly known as Java Mail) is a Jakarta EE API used to send and receive E-Mail. The Java Mail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The Java Mail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform. We can sending and receiving electronic messages, more specifically e-mails are an essential part. Emails are a medium of communication between different parties who are using the application. The standard programming languages, email APIs are available for communication, and Java is also not an exception. Java provides e-mail APIs which are platform and protocol independent. The mail management framework consists of various abstract classes for defining an e-mail communication system.

Maven Dependency <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> </dependency> Java Mail API Architecture

Java mail API comes as a default package with Java EE Platform, and it is optional for Java SE Platform. Java mail framework is composed of multiple components. Java Mail API is one such component used by the developers to build mail applications. But, these APIs are just a layer between the Java Application (Mail Enabled) and the protocol service providers.

Java Mail APIs These are the Java interfaces to send and receive e-mails. This layer is completely independent of the underlying protocols.
Java Beans Activation Framework (JAF) This framework is used to manage mail contents like URL, attachments, mail extensions etc.
Service Provider Interfaces (SPI) This layer sits between the protocol implementers and the Java applications, more specifically Java mail APIs. SPIs understand the protocol languages and hence create the bridge between the two sides.
MIME MIME Stands for Multipurpose Internet Mail Extensions. MIME is a protocol to define the transferred content.

Service Protocol Implementers

These are the third party service providers who implements different protocols like SMTP, POP3 and IMAP etc.
In this context we must have some ideas on the following protocols.

SMTP SMTP Stands for Simple Mail Transfer Protocol. SMTP is used for sending E-Mails.
POP3 POP3 Stands for Post Office Protocol. POP3 is used to receive e-mails. It provides one to one mapping for users and mail boxes, which is one mail box for one user.
IMAP IMAP Stands for Internet Message Access Protocol. IMAP is also used to receive e-mails. It supports multiple mail boxes for single user.
MIME MIME Stands for Multipurpose Internet Mail Extensions. MIME is a protocol to define the transferred content.

Interface in Java Mail API
Interface Description
EncodingAware A DataSource that also implements EncodingAware may specify the Content-Transfer-Encoding to use for its data.
MessageAware An Interface optionally implemented by DataSources to supply information to a DataContentHandler about the message context in which the data content object is operating.
MultipartDataSource MultipartDataSource is a DataSource that contains body parts.
Part The Part interface is the common base interface for Messages and BodyParts.
QuotaAwareStore An interface implemented by Stores that support quotas.
UIDFolder The UIDFolder Interface is implemented by Folders that can support the "disconnected" mode of operation, by providing unique-ids for messages in the folder.

Class in Java Mail API
Class Description
Address This abstract class models the addresses in a message.
Authenticator The class Authenticator represents an object that knows how to obtain authentication for a network connection.
BodyPart This class models a Part that is contained within a Multipart.
FetchProfile Clients use a FetchProfile to list the Message attributes that it wishes to prefetch from the server for a range of messages.
FetchProfile.Item This inner class is the base class of all items that can be requested in a FetchProfile.
Flags The Flags class represents the set of flags on a Message.
Flags.Flag This inner class represents an individual system flag.
Folder Folder is an abstract class that represents a folder for mail messages.
Header The Header class stores a name/value pair to represent headers.
Message This class models an email message.
Message.RecipientType This inner class defines the types of recipients allowed by the Message class.
MessageContext The context in which a piece of Message content is contained.
Multipart Multipart is a container that holds multiple body parts.
PasswordAuthentication The class PasswordAuthentication is a data holder that is used by Authenticator.
Provider The Provider is a class that describes a protocol implementation.
Provider.Type This inner class defines the Provider type.
Quota This class represents a set of quotas for a given quota root.
Quota.Resource An individual resource in a quota root.
Service An abstract class that contains the functionality common to messaging services, such as stores and transports.
Session The Session class represents a mail session and is not subclassed.
Store An abstract class that models a message store and its access protocol, for storing and retrieving messages.
Transport An abstract class that models a message transport.
UIDFolder.FetchProfileItem A fetch profile item for fetching UIDs.
URLName The name of a URL.

Exception in Java Mail API
Exception Description
AuthenticationFailedException This exception is thrown when the connect method on a Store or Transport object fails due to an authentication failure (Example : Invalid User Name or Password).
FolderClosedException This exception is thrown when a method is invoked on a Messaging object and the Folder that owns that object has died due to some reason.
FolderNotFoundException This exception is thrown by Folder methods, when those methods are invoked on a non existent folder.
IllegalWriteException The exception thrown when a write is attempted on a read-only attribute of any Messaging object.
MessageRemovedException The exception thrown when an invalid method is invoked on an expunged Message.
MessagingException The base class for all exceptions thrown by the Messaging classes.
MethodNotSupportedException The exception thrown when a method is not supported by the implementation.
NoSuchProviderException This exception is thrown when Session attempts to instantiate a Provider that doesn‚t exist.
ReadOnlyFolderException This exception is thrown when an attempt is made to open a folder read-write access when the folder is marked read-only.
SendFailedException This exception is thrown when the message cannot be sent.
StoreClosedException This exception is thrown when a method is invoked on a Messaging object and the Store that owns that object has died due to some reason.

Annotation Type in Java Mail API
Annotation Type Description
MailSessionDefinition Annotation used by Java EE applications to define a MailSession to be registered with JNDI.
MailSessionDefinitions Declares one or more MailSessionDefinition annotations.

Standard Properties in Java Mail API

The Java Mail API supports the following standard properties, which may be set in the Session object (or) in the Properties object used to create the Session object.
The Properties are always set as strings, the Type column describes how the string is interpreted.


Name Type Description
mail.debug boolean The initial debug mode. Default is false.
mail.from String The return email address of the current user, used by the InternetAddress method getLocalAddress.
mail.mime.address.strict boolean The MimeMessage class uses the InternetAddress method parseHeader to parse headers in messages. This property controls the strict flag passed to the parseHeader method. The default is true.
mail.host String The default host name of the mail server for both Stores and Transports. Used if the mail.protocol.host property isn‚t set.
mail.store.protocol String Specifies the default message access protocol. The Session method getStore() returns a Store object that implements this protocol. By default the first Store provider in the configuration files is returned.
mail.transport.protocol String Specifies the default message transport protocol. The Session method getTransport() returns a Transport object that implements this protocol. By default the first Transport provider in the configuration files is returned.
mail.user String The default user name to use when connecting to the mail server. Used if the mail.protocol.user property isn‚t set.
mail.protocol.class String Specifies the fully qualified class name of the provider for the specified protocol. Used in cases where more than one provider for a given protocol exists; this property can be used to specify which provider to use by default. The provider must still be listed in a configuration file.
mail.protocol.host String The host name of the mail server for the specified protocol. Overrides the mail.host property.
mail.protocol.port int The port number of the mail server for the specified protocol. If not specified the protocol‚s default port number is used.
mail.protocol.user String The user name to use when connecting to mail servers using the specified protocol. Overrides the mail.user property.