Send emails in Spring Boot

If you are looking for a fast tutorial on how to send emails in Spring Boot applications, then you landed in the right place 😀

1) Add the spring-boot-starter-mail dependency to the pom.xml file


2) Configure a mail server for testing purposes. I would recommend Mailtrap, a free online service for testing email features in your projects. I love using it and you can check out my review of Mailtrap in this article. You will need the following properties:

  • hostname
  • port
  • username
  • password

In Mailtrap, you can grab them in your Test Inbox.

Mailtrap Configuration


3) Add the email server configuration in the file. Here, we will use the Mailtrap config, but the actual values will be different in your application, depending on your email server.

4) Bind the application properties to a configuration class for better reuse in code. This step is optional, but it can simplify the reuse of mail settings across your project.


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

public class EmailCfg {
    private String host;

    private int port;

    private String username;

    private String password;

    public String getHost() {
        return host;

    public void setHost(String host) { = host;

    public int getPort() {
        return port;

    public void setPort(int port) {
        this.port = port;

    public String getUsername() {
        return username;

    public void setUsername(String username) {
        this.username = username;

    public String getPassword() {
        return password;

    public void setPassword(String password) {
        this.password = password;

5) Create a REST controller method that will send emails. You can use any method, but because we are using Spring Boot, the actual code that will send emails is located inside a REST controller.


import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.ValidationException;

public class FeedbackController {

    private EmailCfg emailCfg;

    public FeedbackController(EmailCfg emailCfg) {
        this.emailCfg = emailCfg;

    public void sendFeedback(@RequestBody Feedback feedback,
                             BindingResult bindingResult){
            throw new ValidationException("Feedback is not valid");

        // Create a mail sender
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        // Create an email instance
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("New feedback from " + feedback.getName());

        // Send mail

import javax.validation.constraints.Email;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

public class Feedback {
    private String name;

    private String email;

    private String feedback;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;

    public String getFeedback() {
        return feedback;

    public void setFeedback(String feedback) { = feedback;

That’s it:) You can now send emails by making a REST call to http://localhost:<port>/feedback with the appropriate data.

POST http://localhost:8082/feedback
Content-Type: application/json

  "name": "Dan",
  "email": "",
  "feedback": "You app is awesome :)"