In this post under Java Mail. I will show with an example how to send email using Java 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 this post I will explain one of them. The steps involved in sending a mail are as mentioned below
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
Now I will go through steps in detail
1) Create an instance of java.util.Properties object with empty values.
Properties properties = new Properties();
Usually the properties object is used store JavaMail System properties information such as mail.smtp.host, mail.smtp.ssl.enable, mail.smtp.auth etc. For the approach that we are following we will be providing these information in different methods, so we only need empty Properties instance.
2) Get a email Session object using the Properties instance created at step 1
Session session = Session.getInstance(properties);
A Session object represents a email session. The Properties object created at step 1 will be used to configure the session object, that is why we pass the Properties instance as an argument to Session’s static method “getInstance”.
3) Create an instance of Message abstract class by instantiating a particular concrete implementation. javax.mail.Message is an abstract class and it defines a set of attributes like “Email From Address”, “Email To Address”, “Email Content” etc. For our example we create an instance of MimeMessage which is one of the implementations of Message abstract class. MimeMessage represents Internet email messages.
MimeMessage mimeMessage = new MimeMessage(session);
The constructor of MimeMessage class take Session as constructor argument when creating an instance of MimeMessage.
4) Create an instance of Transport object
The Transport object represents the protocol that will be used to connect to email server and send the email. So while creating an instance of it we need to specify the protocol that we will be using as shown below
Transport t = session.getTransport("smtps");
smtps means we are telling to use Secure SMTP protocol. There is another overloaded version of method named
Session.getTransport()
In this method, we don’t pass the protocol. So which protocol to follow is taken from the System property mail.transport.protocal set in the Properties object created at step 1. Since we are specifying the protocol as method argument we are creating an empty Properties object at step 1.
5) 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);
6) Set the message’s Subject
mimeMessage.setSubject("Welcome");
7) Set the content of the message.
mimeMessage.setText("Welcome to email");
8) Using the Transport object obtained at step 4, connect to SMTP server
t.connect("smtp.gmail.com","xxxxxxxxxxxxx", "xxxxxxxxxxxx");
In the above code we call “connect” method on the Transport object and pass gmail smtp server address, user name and application password. The first argument is
Gmail smtp server address, second argument is user name and the third argument is application password.
9) Send the message to all recipients
Address[] addresses = {to};
t.sendMessage(mimeMessage, addresses);
As shown in the above code, we call “sendMessage” method on the Transport object to send the message.
Below is the complete java code for your reference
Main class
package package1;
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 Example1 {
public static void main(String[] args) {
Properties properties = new Properties();
Session session = Session.getInstance(properties);
session.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(session);
Transport t = null;
try {
t = session.getTransport("smtps");
Address from = new InternetAddress("xxxxxxxxxxxxx@gmail.com");
Address to = new InternetAddress("xxxxxxxxxxx@gmail.com");
mimeMessage.setText("Welcome to email");
mimeMessage.setSubject("Welcome");
mimeMessage.setFrom(from);
mimeMessage.setRecipient(Message.RecipientType.TO, to);
t.connect("smtp.gmail.com","xxxxxxxxxxxx@gmail.com", "xxxxxxxxxx");
Address[] addresses = {to};
t.sendMessage(mimeMessage, addresses);
} catch(MessagingException excep) {
excep.printStackTrace();
} finally {
if(t != null) {
try {
t.close();
} catch(MessagingException excep) {
excep.printStackTrace();
}
}
}
}
}