In this tutorial, I am going to show 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 password mentioned above is an app-specific password, you should have to generate your application password from google here. And you should specify the generated password on the above configurations. 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 went well, you should have to see the Mail Sent Success! Message on your browser. Then check your mail inbox to confirm.

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 application.
    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, I am reading the attachment from the classpath resource. The real-time use case might be upload a document 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 it as a real-time scenario.

Access the below endpoint URL to send mail with attachments:


Spring Boot Mail Example 3

Download Source from GIT:

Happy Learning 🙂