Thursday, April 11, 2013

JavaMail API

Before digging into the code, here are some basic concepts:

Interactions between email servers(software) and clients are governed by email protocols, the most common email protocols are:
  • POP (aka POP3)
  • IMAP (aka IMAP4)
  • MAPI.
  • SMTP


POP
POP version 3, allows email client software to retrieve email from a remote server. Its main characterist is that when the e-mails are seen, those are removed from the server and stored on the user's PC.
 

IMAP
Internet Message Access Protocol, on version 4, IMAP4, allows a local email client to access email messages that reside on a remote server.  The emails reside on the server and only are remove explicity by the user.

IMAP over SSL (IMAPS) uses port number 993.
 

MAPI 
Proprietary email protocol of Microsoft, that can be used by Outlook to communicate with Microsoft Exchange (its email server software).
 

SMPT
Its the only protocol to send emails, POP and IMAP are used to incoming emails and SMTP is for outgoing messages. It is also known as SMTP/MIME.


JavaMail Layered Architecture

In few words, JavaMail API only defines the interfaces, and depending on the protocol, and implementation is required, this is very similar to JDBC and the specific database drivers.

Abstract Layer: declares classes, interfaces and abstract methods intended to support mail handling functions that all mail systems support.


Internet implementation layer: implements part of the abstract layer using internet standards, the service providers  implement the JavaMail API (for each required protocol POP3, SMTP, IMAP)


JavaMail Framework is used for

  1. Create a mail message consisting of a collection of header attributes and a block of data.Create a Session object, which authenticates the user, and controls access to the message store and transport.
  2. Send the message to its recipient list.
  3. Retrieve a message from a message store.

Main Classes

javax.mail.Session 
Require it to get a mail session, you connect to the mail server 

javax.mail.Message
The Message class is an abstract class that defines a set of attributes and a content for a mail message.
It also implements the Part interface, which defines attributes that are required to define and format data content carried by a Message object, and to interface successfully to a mail system.

You send a new message by instantiating an appropriate Message subclass and then you set the attributes such as recipient address, subject, and inserts the content into the Message object.

A Message object can contain multiple parts, where each part contains its own set of attributes and content. The content of a multipart message is a Multipart object that contains BodyPart objects representing each individual Part.

javax.mail.Folder
Messages are stored in Folder objects. A Folder object can contain subfolders as well as messages, thus providing a tree-like folder hierarchy. With this you fetch, append, copy and delete messages.



javax.mail.Transport
Require it to send a message(email) via this class/method send()


References: