Java Mail API Tutorial

Java Mail API Tutorial

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.

Java Mail API Tutorial

The Java Mail is an API that is used to compose, write and read electronic messages (emails).

The Java Mail API provides protocol-independent and plateform-independent framework for sending and receiving mails.

The javax.mail and javax.mail.activation packages contains the core classes of JavaMail API.

The JavaMail facility can be applied to many events. It can be used at the time of registering the user (sending notification such as thanks for your interest to my site), forgot password (sending password to the users email id), sending notifications for important updates etc. So there can be various usage of java mail api.

 Following are some of the protocols supported in JavaMail API:


  • SMTP: Acronym for Simple Mail Transfer Protocol. It provides a mechanism to deliver email.
  • POP: Acronym for Post Office Protocol. POP is the mechanism most people on the Internet use to get their mail. It defines support for a single mailbox for each user. RFC 1939 defines this protocol.
  • IMAP: Acronym for Internet Message Access Protocol. It is an advanced protocol for receiving messages. It provides support for multiple mailbox for each user, in addition to, mailbox can be shared by multiple users. It is defined in RFC 2060.
  • MIME: Acronym for Multipurpose Internet Mail Extensions. . It is not a mail transfer protocol. Instead, it defines the content of what is transferred: the format of the messages, attachments, and so on. There are many different documents that take effect here: RFC 822, RFC 2045, RFC 2046, and RFC 2047. As a user of the JavaMail API, you usually don’t need to worry about these formats. However, these formats do exist and are used by your programs.
  • NNTP and Others:There are many protocols that are provided by third-party providers. Some of them are Network News Transfer Protocol (NNTP), Secure Multipurpose Internet Mail Extensions (S/MIME) etc.


As said above the java application uses JavaMail API to compose, send and receive emails.The following figure illustrates the architecture of JavaMail:

Java Mail API

The abstract mechanism of JavaMail API is similar to other J2EE APIs, such as JDBC, JNDI, and JMS. As seen the architecture diagram above, JavaMail API is divided into two main parts:


  • An application-independent part: An application-programming interface (API) is used by the application components to send and receive mail messages, independent of the underlying provider or protocol used.
  • A service-dependent part: A service provider interface (SPI) speaks the protocol-specific languages, such as SMTP, POP, IMAP, and Network News Transfer Protocol (NNTP). It is used to plug in a provider of an e-mail service to the J2EE platform.

Table of Contents-

  1. JavaMail API Environment Setup
  2. JavaMail API Core Classes
  3. Sending Email JavaMail API
  4. Sending Email through Gmail Server
  5. JavaMail API Receiving email
  6. JavaMail API Sending email with attachment
  7. JavaMail API Receiving email with attachment
  8. JavaMail API Sending email with Html content
  9. JavaMail API  Sending Email With Inline Images
  10. JavaMail API Forwarding Emails
  11. JavaMail API Deleting Emails
  12. JavaMail API Replying Emails
  13. JavaMail API Authentication


<<Previous <<   || Index ||   >>Next >>