How to Send Email Using Java Gmail SMTP Without SSL

How to Send Email Using Java Gmail SMTP Without SSL


In this article, I am going to explaining How to Send Email Using Java Gmail SMTP Without SSL and using TLS.


In the following example, sending an email text body by using java Gmail SMTP Without SSL. i.e TLS

Gmail SMTP TLS Port: 587

Gmail TLS Code

boolean STARTTLS=true;
properties.put("mail.smtp.starttls.enable", STARTTLS);



  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.Message.RecipientType;
import javax.mail.Authenticator;
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 GmailTextMsgSendWithoutSSL {

	Properties properties;
	Session session;
	MimeMessage mimeMessage;

	String USERNAME = "[email protected]";
	String HOSTNAME = "";
	String STARTTLS_PORT = "587";
	boolean STARTTLS = true;
	boolean AUTH = true;

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

	public void sendGmail(String EmailSubject, String EmailBody, String ToAddress) {
		try {
			properties = new Properties();
			properties.put("", HOSTNAME);
			// Setting STARTTLS_PORT
			properties.put("mail.smtp.port", STARTTLS_PORT);
			// AUTH enabled
			properties.put("mail.smtp.auth", AUTH);
			// STARTTLS enabled
			properties.put("mail.smtp.starttls.enable", STARTTLS);

			// 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) {



Gmail SMTP SSL – Sending Email Using JavaMail API

How to send HTML Email Body Using Java Gmail SMTP



You can download the complete source code. Click Here

Thanks for the reading. You can subscribe to get the latest updates.




Leave a Reply