Code Robo
Java Code Complience
Virtual Service
Java Code To Send Email Using Gmail SMTP server
       Talk to EasyAssistant

Java Send Email Functions. It uses GMail smtp server. It shows basic java code to send an email connecting to GMail server. It can be copied and used any other java java project. It uses java mail api class .

Java Send Email File Content:

import java.util.Properties;

import javax.mail.Message;
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 SendMail {

	public static void main(String args[]) {

		SendMail obj = new SendMail();

	public boolean send() {

		boolean sentEmail = false;
		String toEmailAddress = "";
		String fromAddress = "";
		String mailServerName = "";

		Properties properties = System.getProperties();
		properties.put("", mailServerName);
		properties.put("mail.smtp.port", "465");
		// properties.put("mail.smtp.starttls.enable", "true");
		properties.put("mail.smtp.ssl.enable", "true");
		properties.put("mail.smtp.auth", "true");

		 * Get the Session object passing userid and password
		Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("", "*********");


		try {
			// Create a MimeMessage object.
			MimeMessage mimeMessage = new MimeMessage(session);
			mimeMessage.setFrom(new InternetAddress(fromAddress));
			mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmailAddress));
			mimeMessage.setSubject("Attention!! How is your heath today?");
			mimeMessage.setText("We could not trace you/him/her in last 24 hrs");
			System.out.println("Sent message successfully....");
			sentEmail = true;
		} catch (MessagingException mex) {

		return sentEmail;

Note: If you are getting exception to connect and authenticate userid and password. Please following things.
  1. Please double cheeck the user id(email address) and password you are passing.
  2. Login to GMail. Go to gmail settings. Check the checkbox to allow Other program/api to connect gmail.

User Comments:
anonymous (2022-04-16) :
Thanks for providing the sample code. It helped me in our project to do a POC.You are the best!!!
easycodeforall(admim): (2022-04-21)
Thank you for using easycodeforall and posting the comment.
anonymous: (2022-08-31)
easycodeforall: (2022-11-23)
Thank you so much!

anonymous (2022-11-23) :
Do we need to write the main to call these methods?
easycodeforall: (2022-11-23)
Its a sample code. Main method has been added just to test it from eclipse or command line. If you are would like to send the email from an web application (e.g. JSP/Servlet), you do not need to add the main method.

anonymous (2023-07-28) :
Thank you so much for providing details and crispy code!!!!

Post Your Comment:
Name :
Email ( Optional) :
Comments / Suggestion (* Required) It is required: :