How to send mail using Java Mail API?

The Java Mail API provides support for sending and receiving electronic mail messages. The API provides a plug-in architecture where vendor’s implementation for their own proprietary protocols can be dynamically discovered and used at the run time. Sun provides a reference implementation and its supports the following protocols namely,

also read:

  • Internet Mail Access Protocol (IMAP)
  • Simple Mail Transfer Protocol (SMTP)
  • Post Office Protocol 3(POP 3)

In this tip, let us see how to send mail using the Java Mail API. Following is the complete code listing for sending a mail from a Java Application,

package tips.mails;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

	private String from;
	private String to;
	private String subject;
	private String text;

	public SendMail(String from, String to, String subject, String text){
		this.from = from; = to;
		this.subject = subject;
		this.text = text;

	public void send(){

		Properties props = new Properties();
		props.put("", "");
		props.put("mail.smtp.port", "465");

		Session mailSession = Session.getDefaultInstance(props);
		Message simpleMessage = new MimeMessage(mailSession);

		InternetAddress fromAddress = null;
		InternetAddress toAddress = null;
		try {
			fromAddress = new InternetAddress(from);
			toAddress = new InternetAddress(to);
		} catch (AddressException e) {
			// TODO Auto-generated catch block

		try {
			simpleMessage.setRecipient(RecipientType.TO, toAddress);

		} catch (MessagingException e) {
			// TODO Auto-generated catch block

The above program accepts the sender, receiver, subject and the data in its constructor. After giving this information, the mail can be sent by calling the SendMail.send() method. Let us have a deeper look into the implementation of this method.

The first thing is that a Mail Session has to be established with some properties for sending or receiving a mail. The mandatory property is the server name. In the above code, we have given the SMTP Mail server of Google. Then, it is optional to provide the port information, and it is needed if it is different from the default port of 25. Then we construct a Message object for the Mail session by populating the information like sender, receiver, subject and text. Then the message is sent by calling the Transport.send() method.

Let us test the above code by writing a simple test class which is given below,

package tips.mails;

public class SendMailTest {

	public static void main(String[] args) {

		String from = "";
		String to = "";
		String subject = "Test";
		String message = "A test message";

		SendMail sendMail = new SendMail(from, to, subject, message);

also read:

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!