wellnwill

JavaMail

JavaMail is a technology that provides java API to develop java based E-mail client application. It uses various protocols the send and recieve the mails but it is platform and protocol independent.

E-Mail Terminologies

Before looking into JavaMail API and it's feature we must need to familiar with few terminologies used in email application.

E-mail client

An application that is used to compose/create, read and send e-mails knows as e-mail client.

Mail Server

  • An application that receives e-mails from e-mail clients or from another mail server know as mail server.
  • E-mails stores at mail sever can be read, download and deleted by e-mail client.
  • Ex: Microsoft Exchange, WinGate and Microsof Windows POP3

Messaging System

  • An application that sends and receives e-mail from Server. Messaging system is made of many components.
  • Mail User Agent(MUA) - MAU is a client e-mail application that sends and receives messages to or from mail server. Mail User Agent sends message to Messages Transfer Agent.
  • Message Transfer Agent(MTA) - Receives messages from MUA and passes on to another MTA until it reaches to mail server. MTA stores incoming messages and delivers them to recipient. MUA and MTA clubbed together to efficiently send and receive messages to and from mail server. In case of multiple messages, it also queues them before sending. Ex: SandMail and PostFix
  • Message Delivery Agent - Stores messages into e-mail client application mailbox. As we know MTA passes/sends messages to intermediate MTA and again this is passed to next MTA, this journey continue until message reaches to final MTA. The last MTA passes messages to MDA then MDA adds it to client's mailbox or inbox.
  • Message Store - Serves as user mailbox/inbox that holds e-mails until they are read and deleted by the user.
  • SMTP - It's a protocol. It is used to send and receives e-mails over the internet.

There are the various protocols used in JavaMail API to send and receive the e-mails: SMTP, POP3, IMAP, NNTP, MIME

MIME

Multipurpose Internet Mail Extensions (MIME) is an Internet standard that extends the format of email to support:

  • Text in character sets other than ASCII
  • Non-text attachments: audio, video, images, application programs etc.
  • Message bodies with multiple parts
  • Header information in non-ASCII character sets
  • Virtually all human-written Internet email and a fairly large proportion of automated email is transmitted via SMTP in MIME format. Servers insert the MIME header at the beginning of any Web transmission. Clients use this content type or media type header to select an appropriate viewer application for the type of data the header indicates. Some of these viewers are built into the Web client or browser (for example, almost all browsers come with GIF and JPEG image viewers as well as the ability to handle HTML files).

SMTP

Simple Message Transfer Protocol is a internet standard for e-mail transmission across IP networks. SMTP uses TCP port 25 and SMTP allows secured connections through SSL.

The com.sun.mail.smtp package

This package consist of three main classes

  1. SMTPMessage - This class is a specialization of the MimeMessage class that allows you to specify various SMTP options and parameters that will be used when this message is sent over SMTP.
  2. SMTPSSLTransport - This class implements the Transport abstract class using SMTP over SSL for message submission and transport.
  3. SMTPTransport - This class implements the Transport abstract class using SMTP for message submission and transport.

SMTP provider supports following properties which can be set into JavaMail Session object.

  • mail.smtp.host (String) - SMTP Server to connect to.
  • mail.smtp.port (String) - SMTP Server port to connect to. Default is 25.
  • mail.smtp.socketFactory.class (String) - If set, specifies the name of a class that implements the javax.net.SocketFactory interface. This class will be used to create SMTP sockets.
  • mail.smtp.socketFactory.port (int) - Specifies the port to connect to when using the specified socket factory. If not set, the default port will be used.
  • mail.smtp.auth (boolean) - If true, attempt to authenticate the user using the AUTH command. Defaults to false.
  • mail.smtp.starttls.enable (boolean) - If true, enables the use of the STARTTLS command (if supported by the server) to switch the connection to a TLS-protected connection before issuing any login commands. Defaults to false.

Example - code snippet

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
  protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
  }
});

POP3

Post Office Protocol (POP) is an application-layer Internet standard protocol used by local e-mail clients to retrieve e-mail from a remote server over a TCP/IP connection. POP supports simple download-and-delete requirements for access to remote mailboxes. A POP3 server listens on well-known port 110.

The com.sun.mail.pop3 package

  1. POP3Folder - A POP3 Folder (can only be "INBOX").
  2. POP3Message - A POP3 Message.
  3. POP3SSLStore - A POP3 Message Store using SSL.
  4. POP3Store - A POP3 Message Store.

POP3 provider supports following propertiea that can set into JavaMail Session object:

  • mail.pop3.host (String) - POP3 Server to connect to.
  • mail.pop3.port (int) - The POP3 server port to connect to, if the connect() method doesn't explicitly specify one. Defaults to 110.
  • mail.pop3.starttls.enable (boolean) - If true, requires the use of the STLS command. If the server doesn't support the STLS command, or the command fails, the connect method will fail. Defaults to false.

Example - code snippet

Properties properties = new Properties();

properties.put("mail.store.protocol", "pop3");
properties.put("mail.pop3.host", pop3Host);
properties.put("mail.pop3.port", "995");
properties.put("mail.pop3.starttls.enable", "true");

Session emailSession = Session.getDefaultInstance(properties);

SSL

Secure Sockets Layer (SSL) is a cryptographic protocol that enables secure communications over the Internet. SSL was originally developed by Netscape and released as SSL 2.0 in 1995. A much improved SSL 3.0 was released in 1996. Current browsers do not support SSL 2.0.

TLS

Transport Layer Security (TLS) is the successor to SSL. TLS 1.0 was defined in RFC 2246 in January 1999. The differences between TLS 1.0 and SSL 3.0 were significant enough that they did not interoperate. TLS 1.0 did allow the ability to downgrade the connection to SSL 3.0. TLS 1.1 (RFC 4346, April 2006) and TLS 1.2 (RFC 5246, August 2008) are the later editions in the TLS family. Current browsers support TLS 1.0 by default and may optionally support TLS 1.1 and 1.2

 
Footer with Map

Top