How to Send Email Using Java Gmail SMTP With SSL

How to Send Email Using Java Gmail SMTP With SSL

In this article, I am going to explaining How to Send Email Using Java Gmail SMTP With SSL. SSL is a secure layer. To send email insecurely, have to use Gmail SSL port to establish a Gmail SMTP connection.


In the following, sending an email text body by using java Gmail SMTP With SSL.

Gmail SMTP SSL Port: 465

Gmail SSL Code

properties.put("mail.smtp.socketFactory.class", "");


  1. 1. Java 1.8
  2. 2. Eclipse IDE

In eclipse, Create maven project and add the Java mail API dependency jar in pom.xml as follows


<project xmlns=""





		<!-- -->




import java.util.Properties;

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

public class GmailTextMsgSendWithSSL {

	Properties properties;
	Session session;
	MimeMessage mimeMessage;

	String USERNAME = "[email protected]";
	String HOSTNAME = "";
	String SSL_PORT = "465";
	boolean AUTH = true;
	String SERVERTYPE = "smtp";

	public static void main(String args[]) throws MessagingException {
		String EmailSubject = "Subject:Text Subject";
		String EmailBody = "Text Message Body: Hello World";
		String ToAddress = "[email protected]";

		GmailTextMsgSendWithSSL gmailTextMsgSend = new GmailTextMsgSendWithSSL();
		gmailTextMsgSend.sendGmail(EmailSubject, EmailBody, ToAddress);

	public void sendGmail(String EmailSubject, String EmailBody, String ToAddress) {
		try {
			properties = new Properties();
			properties.put("", HOSTNAME);

			// SSL_PORT Enabled
			properties.put("mail.smtp.port", SSL_PORT);

			// AUTH Enabled
			properties.put("mail.smtp.auth", AUTH);

			// SSL Enabled
			properties.put("mail.smtp.socketFactory.port", SSL_PORT);
			properties.put("mail.smtp.socketFactory.class", "");

			// Authenticating
			Authenticator auth = new Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(USERNAME, PASSWORD);

			// creating session
			session = Session.getDefaultInstance(properties, auth);

			// create mimemessage
			mimeMessage = new MimeMessage(session);
			mimeMessage.addRecipient(RecipientType.TO, new InternetAddress(ToAddress));

			// setting text message body

			// sending mail
			System.out.println("Mail Send Successfully");

		} catch (Exception e) {



Some other important links

Gmail SMTP SSL – Sending Email Using JavaMail API

How to send HTML Email Body Using Java Gmail SMTP

Thanks for reading. You can subscribe to get more updates.



Leave a Reply