Sending Mail From Java using Gmail

Sending E-Mail From Java with Gmail:


We can send emails from java using gmail ids. Please find the below working java code to send the emails from your java program with your existing gmail ids.

Required Jar:

javax.mail

Gmail configurations to allow sending the emails from Java Program.

  1. Make sure you don’t have two step authentication enabled for the email which you are planning to send the email from your java program.
  2. Enable the less secure apps true in this link

SMTP email Configuration for Gmail:

Properties props = new Properties();
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.host", "smtp.gmail.com");
			props.put("mail.smtp.port", "587");

			Session session = Session.getInstance(props, new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("admin@example.com", "guru#2013");
				}
			});

Mail Compose from Java – Code Snippet

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("admin@example.com"));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("admin@ngdeveloper.com"));
			message.setSubject("Testing Subject");
			message.setText("Testing Text");
			Transport.send(message);

Sending Email from Java Program using Gmail Id – Full Executable Program:

package com.ngdeveloper;

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

		final String fromEmailId = "gmail-username";
		final String fromEmailPwd = "gmail-password";
		final String toEmailId = "to email id";
		final String mailSub = "Ngdeveloper.com";
		final String mailContent = "Ngdeveloper.com is a website where you can get many programming tutorials,"
				+ "\n\n And solutions for many programming problems!";

		Properties gmailProps = new Properties();
		gmailProps.put("mail.smtp.auth", "true");
		gmailProps.put("mail.smtp.starttls.enable", "true");
		gmailProps.put("mail.smtp.host", "smtp.gmail.com");
		gmailProps.put("mail.smtp.port", "587");

		System.out.println("Authenticating username and password");
		Session session = Session.getInstance(gmailProps, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(fromEmailId, fromEmailPwd);
			}
		});
		System.out.println("Authentication Successful!");

		try {
			Message message = new MimeMessage(session);
// To Email Id set here
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmailId));
// Subject set here
			message.setSubject(mailSub);
// Mail content set here
			message.setText(mailContent);
			Transport.send(message);
			System.out.println("Mail Sent Successfully");
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}

Sending Email from Gmail – Java Program output

Authenticating username and password
Authentication Successful!
Mail Sent Successfully

Leave a Reply