VOOZH about

URL: https://www.geeksforgeeks.org/springboot/spring-boot-sending-email-via-smtp/

⇱ Spring Boot - Sending Email via SMTP - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Boot - Sending Email via SMTP

Last Updated : 12 May, 2026

Spring Boot provides built-in support for sending emails using the JavaMail API through the Simple Mail Transfer Protocol (SMTP) server. Using the spring-boot-starter-mail dependency, developers can easily configure and send emails from Spring Boot applications.

  • Supports sending simple text emails and emails with attachments.
  • Email configuration can be easily defined using application.properties.

Step-by-Step Implementation

Follow the steps below to create a Spring Boot application that sends emails using Gmail SMTP.

Step 1: Create a Spring Boot Project

Create a new project using Spring Initializr.

Project Configuration:

  • Project: Maven
  • Language: Java
  • Spring Boot Version: Latest stable version
  • Group: com.gfg
  • Artifact: springBootEmailProject
  • Packaging: Jar
  • Java Version: 17 or higher

Add Dependencies:

Select the following dependency:

  • Spring Web
  • Spring Boot Starter Mail

Download the project, extract it, and open it in your IDE.

Step 2: Configure Gmail SMTP in application.properties

Add the following configuration in the application.properties file.

The Gmail ID used to log in to your Gmail account can be provided as the username. To generate the password, 2-step verification must be enabled for your Gmail account.

πŸ‘ Image

Steps to generate App Password:

  1. Login to Gmail
  2. Manage your Google Account
  3. Security
  4. Enable 2-Step Verification
  5. Go to App Passwords
  6. Select app with a custom name
  7. Click Generate

Use the generated password in the application.properties file.

πŸ‘ Image

Note: For reference,

If you don’t find the App Password option under Security even after enabling 2-step verification, you might be encountering an issue detailed in this Google Support Thread on App Passwords.

To generate an App Password, visit the Google App Passwords page. This page will guide you through the process of creating an App Password for your Gmail account.

Step 3: Create EmailDetails Class

Create a class EmailDetails to store email details such as recipient, subject, message body, and attachment.

Step 4: Create EmailService Interface

Create a service interface EmailService that defines methods for sending emails.

The EmailService interface defines two methods:

  1. String sendSimpleMail(EmailDetails details): This method can be used to send a simple text email to the desired recipient.
  2. String sendMailWithAttachment(EmailDetails details): This method can be used to send an email along with an attachment to the desired recipient.

The Interface and service implementation class is as shown below in example as follows: 

EmailService.java

The sendSimpleMail() method is used to send a simple text email, while the sendMailWithAttachment() method is used to send an email with an attachment.

Step 5: Implement EmailService

Create the service implementation class EmailServiceImpl.

The JavaMailSender interface is used to send emails. SimpleMailMessage is used for sending simple text emails, while MimeMessage is used to send emails with attachments.

Step 6: Create EmailController

Create a REST controller EmailController that exposes APIs for sending emails.

Step 7: Run the Spring Boot Application

Run the Spring Boot application and send a request using Postman:

  • Method: POST

URL: http://localhost:8080/sendMail

JSON:

{

"recipient": "recipient@gmail.com",

"msgBody": "Hey,\n\nThis is a Simple Email.\n\nThanks",

"subject": "Simple Email Message"

}

πŸ‘ Image

Mail received on Gmail is as follows:

πŸ‘ Image

Step 8: Send Email With Attachment

Run the Spring Boot application and send a request to:

http://localhost:8080/sendMailWithAttachment

πŸ‘ Image

Mail received on Gmail is as follows:

πŸ‘ Image

Comment

Explore