Java Mail API Tutorial

Java Mail API is a set of abstract  APIs  in Java EE which enables a java application to send and receive emails , along with a Java Mail Service provide Implementation(Java Mail SPI).This Java Mail API tutorial discusses the various concepts of Java Mail API with suitable examples.Java Mail API is embedded with Java EE. It is possible to add Java mail with Java SE (Please visit this page  for more details).

Java Mail API Architecture

As the name suggests , it is an API to interact with mail servers.This API does not contain any mail servers in it.Instead , it contains interfaces and classes for interacting with mail servers.The Java mail API along with a Service Provider Implementation(SPI) makes a Java application to be able to send and receive mails.Java EE  is shipped with a default SPI . If we want we can use other SPIs like  GNU Java Mail.

Block Diagram of Java Mail API

The block diagram of a Java mail API system is shown below.

Java Mail API supports the following protocols:

1)SMTP(Simple Mail Transfer protocol)

2)POP(Post Office Protocol) – An email retrieval protocol

3)IMAP(Internet Message Access Protocol) – An email retrieval protocol.

Of the above 3 , SMTP can be used to send and retrieve mails with mail server.(Generally email clients uses SMTP only to send mails.To receive mails , they are using POP or IMAP.)POP and IMAP are using only to retrieve mails from mail server.

An open source Java Mail  implementation is there. It is the GNU Java Mail .It is an implementation of Java Mail API specification version 1.2. It  gives NNTP (Network News Transfer Protocol)support.

Major Components of Java Mail API

There are many components in Java Mail API.We are discussing very few here.Other components will be discussing in coming chapters.

1)The Message class

This class is an implementation of the Part interface . The Part interface defines  attributes that are required to define a Message object. In addition the Message class adds few more attributes like From, To , Subject,Reply-To .

2)The Transport class

This class provides the send() method . It is using to send the email.

3)The Session class

This class defines the user related properties as well as global properties.(For examples user related properties like id and password)

See Related :

SMTP

Sending email in Java using SMTP

Sending email with attachment

POP

Reading email using POP