Sending Email Using SMTP (Approach 2)

In this post under Java Mail. I will show with an example how to send email using Java Mail api and SMTP protocol.

Note: For this example and other examples in future we need to have
1) Gmail account
2) GMail SMTP server address
3) Stop the antivirus software running on your local.
4) Create an application password. The steps are mentioned in the url https://support.google.com/accounts/answer/185833?p=InvalidSecondFactor

They are two approaches to send mail, in the previous post I followed the below steps

1) Create an instance of java.util.Properties object with empty values.
2) Get a email Session object using the Properties instance created at step 1
3) Create an instance of Message abstract class by instantiating a particular concrete implementation.
4) Create an instance of Transport object
5) Set the message’s From and To address
6) Set the message’s Subject
7) Set the content of the message.
8) Using the Transport object obtained at step 4, connect to SMTP server
9) Send the message to all recipients

In this post I will explain the second one. The steps involved in sending a mail are as mentioned below

1) Create an instance of java.util.Properties object with values for system properties “mail.smtp.hot” and “mail.smtp.ssl.enable”.
2) Get a email Session object using the Properties instance created at step 1
3) Create an instance of Message abstract class by instantiating a particular concrete implementation.
4) Set the message’s From and To address
5) Set the message’s Subject
6) Set the content of the message.
7) Using the Transport class static method “send”, we send the message to all recipients.

If you compare the steps from the approach 1 and approach 2.

The approach 2 has less steps. The steps 4, 8, and 9 are replaced by only one step i.e., 7 in approach 2.

Now I will go through steps in detail

1) Create an instance of java.util.Properties object with values for system properties “mail.smtp.hot” and “mail.smtp.ssl.enable”.


	Properties properties = new Properties();
	properties.put("mail.smtp.host", "smtp.gmail.com");
	properties.put("mail.smtp.ssl.enable", "true");

2) Get a email Session object using the Properties instance created at step 1

Session session = Session.getInstance(properties);

3) Create an instance of Message abstract class by instantiating a particular concrete implementation.

MimeMessage mimeMessage = new MimeMessage(session);

4) Set the message’s From and To address
To set a message’s From and To address, we need to create From and To address. We will use an instance of InternetAddress to represent “From Address” and “To Address” as shown below. An InternetAddress represents a user’s email address.


	Address from = new InternetAddress("xxxxxxxxx@gmail.com");
	Address to = new InternetAddress("xxxxxxxxx@gmail.com");

Next we set the message’s from and to addresses as shown below


	mimeMessage.setFrom(from);
	mimeMessage.setRecipient(Message.RecipientType.TO, to);

5) Set the message’s Subject

mimeMessage.setSubject("Welcome");

6) Set the content of the message.

mimeMessage.setText("Welcome to email");

7) Using the Transport class static method “send”, we send the message to all recipients.

Transport.send(mimeMessage, "xxxxxxxxxx@gmail.com", "xxxxxxxxxxxxxxxx");

This method internally creates a secured SMTP Transport object using the System properties set in Step 1. Secured in the sense ssl secured because we enabled the
system property “mail.smtp.ssl.enable”. It uses smtp protocol because we mentioned it to use smtp protocol through the system property “mail.smtp.host”.

Using the transport object it connects to Gmal SMTP server using the credentials passed as arguments to send method.

Then sends the message.

This is exactly equal to steps 4, 8, and 9 from Approach 1. The static “send” method is shortcut to steps 4, 8, and 9 from Approach 2.

Below is the full java code for your reference

Main Class


package package2;
import java.util.Properties;

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

public class Example2 {
	public static void main(String[] args) {
		Properties properties = new Properties();
		properties.put("mail.smtp.host", "smtp.gmail.com");
		properties.put("mail.smtp.ssl.enable", "true");
		Session session = Session.getInstance(properties);
		session.setDebug(true);
		MimeMessage mimeMessage = new MimeMessage(session);
		
		try {
			Address from = new InternetAddress("sumanth.prabhakar@gmail.com");
			Address to = new InternetAddress("ksprao16@gmail.com");
			
			mimeMessage.setText("Welcome to email");
			mimeMessage.setSubject("Welcome");
			mimeMessage.setFrom(from);
			mimeMessage.setRecipient(Message.RecipientType.TO, to);
			
			Transport.send(mimeMessage, "sumanth.prabhakar@gmail.com", "avuahgdxziiezhpb");
		} catch(MessagingException excep) {
			excep.printStackTrace();
		}
	}
}

Leave a Reply