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.
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your_email@gmail.com
spring.mail.password=your_app_password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
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.

Steps to generate App Password:
- Login to Gmail
- Manage your Google Account
- Security
- Enable 2-Step Verification
- Go to App Passwords
- Select app with a custom name
- Click Generate
Use the generated password in the application.properties file.
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.
package com.SpringBootEmail.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmailDetails {
private String recipient;
private String msgBody;
private String subject;
private String attachment;
}
Step 4: Create EmailService Interface
Create a service interface EmailService that defines methods for sending emails.
The EmailService interface defines two methods:
- String sendSimpleMail(EmailDetails details): This method can be used to send a simple text email to the desired recipient.
- 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
package com.SpringBootEmail.service;
import com.SpringBootEmail.entity.EmailDetails;
public interface EmailService {
// Method to send simple email
String sendSimpleMail(EmailDetails details);
// Method to send email with attachment
String sendMailWithAttachment(EmailDetails details);
}
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.
package com.SpringBootEmail.service;
import com.SpringBootEmail.entity.EmailDetails;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@Service
public class EmailServiceImpl implements EmailService {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String sender;
// Send simple mail
public String sendSimpleMail(EmailDetails details) {
try {
SimpleMailMessage mailMessage =
new SimpleMailMessage();
mailMessage.setFrom(sender);
mailMessage.setTo(details.getRecipient());
mailMessage.setText(details.getMsgBody());
mailMessage.setSubject(details.getSubject());
javaMailSender.send(mailMessage);
return "Mail Sent Successfully";
} catch (Exception e) {
return "Error while sending mail";
}
}
// Send mail with attachment
public String sendMailWithAttachment(
EmailDetails details) {
MimeMessage mimeMessage =
javaMailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper =
new MimeMessageHelper(mimeMessage, true);
helper.setFrom(sender);
helper.setTo(details.getRecipient());
helper.setText(details.getMsgBody());
helper.setSubject(details.getSubject());
FileSystemResource file =
new FileSystemResource(
new File(details.getAttachment()));
helper.addAttachment(
file.getFilename(), file);
javaMailSender.send(mimeMessage);
return "Mail Sent Successfully";
} catch (MessagingException e) {
return "Error while sending mail";
}
}
}
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.
package com.SpringBootEmail.controller;
import com.SpringBootEmail.entity.EmailDetails;
import com.SpringBootEmail.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class EmailController {
@Autowired
private EmailService emailService;
// Send simple email
@PostMapping("/sendMail")
public String sendMail(
@RequestBody EmailDetails details) {
return emailService.sendSimpleMail(details);
}
// Send email with attachment
@PostMapping("/sendMailWithAttachment")
public String sendMailWithAttachment(
@RequestBody EmailDetails details) {
return emailService
.sendMailWithAttachment(details);
}
}
Step 7: Run the Spring Boot Application
Run the Spring Boot application and send a request to:
http://localhost:8080/sendMail
to send a simple email. The email will be delivered to the recipient’s Gmail inbox.

Mail received on Gmail is as follows:
Step 8: Send Email With Attachment
Run the Spring Boot application and send a request to:
http://localhost:8080/sendMailWithAttachment

Mail received on Gmail is as follows: