Java Mail API

by | Dec 7, 2020 | Java

Home » Java » Java Mail API


Suggestive from the name of the Application Programming Interface, the JAVA Mail API is used to read, write, and exchange electronic mails to and from a JAVA program or application. The API provides users with several interfaces, classes, methods, and variables to make email transactions possible.

What is JAVA Mail API?

As defined by the Java Enterprise Edition, “The Java Mail API provides a platform-independent and protocol-independent framework to build mail and messaging applications”. The entire set of classes and methods needed for the process are available in separate packages that can be invoked by importing them onto your program. The packages available for handling mail are as follows:

  • mail – This is the main API that has to be imported if you want to model a mailing system
  • mail.event – This package acts as the listener to record events for the Mail API
  • mail.internet – Required to access mail systems online.
  • – If you want to search through your inbox with a keyword
  • mail.util – This invokes all of the API utility classes that aid in various mail-related tasks.

Block Diagram

Java Mail API

Protocols in Java Mail API

  • SMTP – SMTP stands for Simple Mail Transfer Protocol. This is the fundamental protocol to send emails. This protocol has been exemplified in the code below.
  • POP – POP Stands for Post Office Protocol. This protocol mainly deals with receiving mails and works well with users using a single mailbox.
  • IMAP – IMAP stands for Internet Message Access Protocol. It is one of the more advanced protocols that support multiple mailboxes at a time.
  • MIME – MIME stands for Multiple Internet Mail Extension. This is required when you want to send attachments in the form of videos, images, media, etc.

Advantages of JAVA Mail API

When there is a perfectly well-structured Mail API designed to create a mailing system, it will only be right that we use it in our programs. You will be able to read, compose, and send emails using this construct. The Mail API can also be attached to other applications to facilitate the process of sending mail confirmations or otherwise.


While there are no significant disadvantages to this programming interface, the only thing that might be a concern is memory efficiency. Though not significant enough, codes might take a little longer to compile, and using the Mail API may take up a lot of heap space allocated for JAVA.

Example and Code

/* This example is of a real mailing system that we will develop in JAVA. In this system, a mail can be sent from a sender to a recipient provided that we have the email ID and password of the sender’s account. */
//For the purpose of this example, we will be using an imaginary email id called ‘’ //and its password will be ‘1234’.
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class sendMail
private void toSendMail()

String recMailId = email.getText();
final String username = “”;
final String password = “1234”;
Properties props = new Properties();
props.put(“mail.smtp.starttls.enable”, “true”);
props.put(“mail.smtp.auth”, “true”);
props.put(“”, “”);
props.put(“mail.smtp.port”, “587”);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswprdAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
} )
//This try block will contain the message structure and the contents
Message message = new MimeMessage (session);
//Setting sender
message.setFrom (new InternetAddress(“”));
//Setting Recipients
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recMailId));
//This will show as the subject of the mail
message.setSubject(“New Mail”);
//This is the body of the mail
message.setText(“The test mail has been sent successfully”);

System.out.println(“Task successfully”);
catch (MessagingException e) {
throw new Runtime Exception (e);



Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.