How to Send Mail Spring Boot Example

By | 2018-11-11T10:41:10+00:00 January 1st, 2018|Spring Boot|3 Comments

In this tutorial, I am going to show you how to send mail with Spring Boot rest service.

Technologies used :

  • Spring_Boot_1.5.9.RELEASE
  • Spring-Boot-Starter-Mail
  • Java 8

Spring Boot Mail Dependency :

To send a mail from spring boot application we should add the below dependency in pom.xml

mail dependency in pom.xml


Spring Boot Mail Example :

Here I am going to implement a Simple Spring Boot Example to send a mail and also to send attachments through the mail.

Project Structure :

Spring Boot Mail Example
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


<description>Spring Boot Mail Example</description>

<relativePath />




Configure SMTP Properties in Spring Boot
spring.mail.password=password (App-Password generated from google tool)

Note : The above-mentioned password for application is an app specific password, you should generate your own application password from google here. And you should mention that App Password. For more details on Gmail SMTP go through this document.

Create Spring Rest Controller to implement mail sending logic :

SimpleMailController :
package com.onlinetutorilspoint;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class SimpleMailController {
    private JavaMailSender sender;

    public String sendMail() {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);

        try {
            helper.setText("Greetings :)");
            helper.setSubject("Mail From Spring Boot");
        } catch (MessagingException e) {
            return "Error while sending mail ..";
        return "Mail Sent Success!";
package com.onlinetutorilspoint;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Run the Application :

D:\> mvnw spring-boot:run

Or (Ubuntu)

$ mvn spring-boot:run

Now you can access the below endpoint URL for send Mail :


If everything done well yo can see the Mail Sent Success! message on your browser. And check your mail inbox.

Spring Boot Mail Example 2

Spring Boot Mail With Attachment :

We can also send the email with attachments like below. Add the below method to Controller and run the aapplication.
    public String sendMailAttachment() throws MessagingException {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        try {
            helper.setText("Greetings :)\n Please find the attached docuemnt for your reference.");
            helper.setSubject("Mail From Spring Boot");
            ClassPathResource file = new ClassPathResource("document.PNG");
            helper.addAttachment("document.PNG", file);
        } catch (MessagingException e) {
            return "Error while sending mail ..";
        return "Mail Sent Success!";

Use the MimeMessageHelper(MimeMessage mimeMessage, boolean multipart) to enable multipart mode.

Note : To make this example as simple as possible, I am passing the attachment by reading from classpath itself. Actually, these attachments can be uploaded from the client (Browser) by using file upload functionality. I have discussed how to upload files from spring boot in previous tutorials. You can integrate these two examples and make a real-time scenario.

Access the below endpoint URL to send mail with attachments :


Spring Boot Mail Example 3

Happy Learning 🙂

Download Example

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,


    sunith August 23, 2018 at 11:37 am - Reply

    how can we send through our form

    oumayma August 24, 2018 at 2:42 pm - Reply

    I have this problem when i access this URL : http://localhost:8080/sendMail and I receive nothing in the mail
    please help me 🙁

    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG SMTP: trying to connect to host “”, port 587, isSSL false
    220 ESMTP d1-v6sm3890953wrc.52 – gsmtp
    DEBUG SMTP: connected to host “”, port: 587

    EHLO DESKTOP-J3P2SU8 at your service, []
    250-SIZE 35882577
    250 SMTPUTF8
    DEBUG SMTP: Found extension “SIZE”, arg “35882577”
    DEBUG SMTP: Found extension “8BITMIME”, arg “”
    DEBUG SMTP: Found extension “STARTTLS”, arg “”
    DEBUG SMTP: Found extension “ENHANCEDSTATUSCODES”, arg “”
    DEBUG SMTP: Found extension “PIPELINING”, arg “”
    DEBUG SMTP: Found extension “SMTPUTF8”, arg “”
    220 2.0.0 Ready to start TLS

    VuThuy November 19, 2018 at 10:02 pm - Reply

    Thanks you.

Leave A Comment