Over a million developers have joined DZone.

Sending Mail Using the JavaMail API for Gmail [Snippet]

DZone 's Guide to

Sending Mail Using the JavaMail API for Gmail [Snippet]

This handy snippet will show you how to use the JavaMail API to send email from a Gmail account.

· Java Zone ·
Free Resource

I was recently trying to experiment with an automation framework where I wanted a function in my framework to send email to recipients based on some results.

Since I did not have an SMTP server with me, I thought I'd to use my personal Gmail account for my experiments. Below is a code snippet to trigger mail from a program.

Before moving ahead, be sure to follow the steps below.

  1. Log into your Google account (if you have multiple accounts, be sure that you are signed into the account you wish to send emails from).

  2. Open the following link and toggle the 'Allow less secure apps' option to ON. This is telling your Gmail server to allow it to be accessed by less secured apps.

  3. Now try to below code and use your username and password as needed.

  4. Before everything else, add the JavaMail JAR (latest version as of this article: javax.mail-1.6.1) to your project library. Or, if you are using Maven for project management, add the dependency below to your pom.xml.


package com.test.reportGenertors

import java.text.SimpleDateFormat;
import java.util.Date;
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 Report {

    public void sendMail() {
        //Setting up configurations for the email connection to the Google SMTP server using TLS
        Properties props = new Properties();
        props.put("mail.smtp.host", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        //Establishing a session with required user details
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("youremail@gmail.com", "yourpassword");
        try {
            //Creating a Message object to set the email content
            MimeMessage msg = new MimeMessage(session);
            //Storing the comma seperated values to email addresses
            String to = "recepient1@email.com,recepient2@gmail.com";
            /*Parsing the String with defualt delimiter as a comma by marking the boolean as true and storing the email
            addresses in an array of InternetAddress objects*/
            InternetAddress[] address = InternetAddress.parse(to, true);
            //Setting the recepients from the address variable
            msg.setRecipients(Message.RecipientType.TO, address);
            String timeStamp = new SimpleDateFormat("yyyymmdd_hh-mm-ss").format(new Date());
            msg.setSubject("Sample Mail : " + timeStamp);
            msg.setSentDate(new Date());
            msg.setText("Sampel System Generated mail");
            msg.setHeader("XPriority", "1");
            System.out.println("Mail has been sent successfully");
        } catch (MessagingException mex) {
            System.out.println("Unable to send an email" + mex);

java mail api ,testing automation ,gmail ,java

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}