Send email via Spring – MailSender and SimpleMailMessage


This tutorial will show you how to send a basic mail via Spring framework’s email support. The Spring Framework provides a helpful utility library for sending email that shields the user from the specifics of the underlying mailing system and is responsible for low level resource handling on behalf of the client.

The central interface for sending emails is the MailSender interface; a simple value object encapsulating the properties of a simple mail such as from and to (plus many others) is the SimpleMailMessage class.

This package also contains a hierarchy of checked exceptions which provide a higher level of abstraction over the lower level mail system exceptions with the root exception being MailException.


Eclipse 2019-12, Java at least 1.8, Gradle 6.4.1, Maven 3.6.3, Spring Boot Mail Starter 2.3.1, Java mail API 1.6.2

Project Setup

Create either gradle or maven based project in Eclipse. The name of the project is spring-email-mailsender-and-simplemailmessage.

If you are creating gradle based project then you can use below build.gradle script:

buildscript {
	ext {
		springBootVersion = '2.3.1.RELEASE'
    repositories {
    dependencies {

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {

If you are creating maven based project then you can use below pom.xml file:

<project xmlns=""





					<source>at least 8</source>
					<target>at least 8</target>

Email Configuration

We will create an email configuration class where we will configure SMTP (Simple Mail Transfer Protocol) server details, from email address.


import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

public class EmailConfig {

	public MailSender mailSender() {
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

		mailSender.setPassword("gmail password");

		Properties javaMailProperties = new Properties();
		javaMailProperties.put("mail.smtp.auth", true);
		javaMailProperties.put("mail.smtp.starttls.enable", true);


		return mailSender;

	public MailMessage mailMessage() {
		return new SimpleMailMessage();


We need to issue STARTTLS command otherwise you will get the error message similar to the following:

#smtplib.SMTPSenderRefused: (530, b'5.7.0 Must issue a STARTTLS command first. p7sm24605501pfn.14 - gsmtp', '')

You will get below error if your security level is high:

#smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 x10sm26098036pfn.36 - gsmtp')

Therefore you need to lower your Gmail’s security settings.

Email Sender

Email sender class just does the right job for sending the email to the intended recipient(s).


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;

public class EmailSender {

	private MailSender mailSender;

	private MailMessage mailMessage;

	public void sendEmail(final String subject, final String message, final String[] emailAddresses) {
		try {
			mailSender.send((SimpleMailMessage) mailMessage);
			System.out.println("Email sending complete.");
		} catch (Exception e) {


Main Class

A class is having main method with @SpringBootApplication annotation is enough to start up the Spring Boot application.

In this class we specify the subject, message and the recipient(s) to send the email.


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SimpleMailMessageSenderApp implements CommandLineRunner {

	private EmailSender emailSender;

	public static void main(String[] args) {, args);

	public void run(String... args) throws Exception {
		emailSender.sendEmail("Test Subject", "Test Message", new String[] { "" });


Testing the Application

Now when you execute the main class you will see the following output in the console:

Email sending complete.

If you are sending the email to yourself then you can check your mailbox whether you have received or not. If you don’t receive in inbox then you can search in the spam folder.

Here I am sending myself, so here is the output of the sent email.

mailsender simplemailmessage email spring

Message details:

mailsender simplemailmessage email spring

Source Code


Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *