Sending Emails through Spring Boot Application

    Sending Emails through Spring Boot Application

    Learn How to Send Emails in Spring Boot with JavaMailSender. Step-by-step guide to implement email integration using Spring Boot Starter Mail for real-world applications.

    default profile

    Munaf Badarpura

    July 05, 2025

    3 min read

    Sending emails is a common requirement in real-world applications, whether it’s for account verification, password reset links, order confirmations, or simple notifications. Luckily, Spring Boot makes sending emails easy with minimal setup.

    In this article, I will show you how to send emails using Spring Boot step by step. Whether you’re building a personal project or working on an enterprise application, this guide will help you integrate email functionality quickly.

    Why Send Emails in Applications?#

    Here are some common use-cases for sending emails:

    1. Account verification during user signup
    2. Password reset links for forgot password
    3. Order or booking confirmations
    4. Promotional or marketing emails
    5. System alerts or notifications

    Spring Boot, with the help of JavaMailSender interface, simplifies the process of sending emails from your application.

    Here is Step-by-Step Guide to Send Emails with Spring Boot#

    Add Spring Boot Mail Dependency#

    First, add the Spring Boot Starter Mail dependency to pom.xml:

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>

    If you’re using Gradle:

    implementation 'org.springframework.boot:spring-boot-starter-mail'

    Configure Email Properties#

    In your application.properties or application.yml file add your email server configurations. For this example, we will use Gmail SMTP, but you can use other providers as well.

    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

    Lets understand these configurations:#

    1. spring.mail.host=smtp.gmail.com : this tells Spring Boot which mail server to use. Here, we are using Gmail’s SMTP server, if you are using yahoo use, smtp.mail.yahoo.com.
    2. spring.mail.port=587 : the port number used for email transmission. Port 587 is the standard port for sending emails with STARTTLS security.
    3. spring.mail.username=your_email@gmail.com : This is your Gmail account id that will be used to send emails.
    4. spring.mail.password=your_app_password : the password for your email account, here If you're using Gmail, you can’t use your normal account password here. You need to generate an App Password from your Google account settings for this to work securely.
    5. spring.mail.properties.mail.smtp.auth=true : It means your application will provide a username and password to log in to the mail server.
    6. spring.mail.properties.mail.smtp.starttls.enable=true : This enables STARTTLS, which adds a layer of security by encrypting the connection between your application and the mail server.

    Create Email Service to Send Emails#

    We’ll create a simple service class to send emails using JavaMailSender.

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender mailSender; public void sendSimpleEmail(String toEmail, String subject, String body) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("your_email@gmail.com"); // sender's email message.setTo(toEmail); message.setSubject(subject); message.setText(body); mailSender.send(message); System.out.println("Email sent successfully to " + toEmail); } }

    Create a REST API to Trigger Email#

    We can expose an endpoint to trigger the email for testing purposes.

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api/email") public class EmailController { @Autowired private EmailService emailService; @PostMapping("/send") public String sendEmail(@RequestParam String toEmail, @RequestParam String subject, @RequestParam String body) { emailService.sendSimpleEmail(toEmail, subject, body); return "Email sent successfully!"; } }

    You can now test the API using Postman or your browser using this API:

    http://localhost:8080/api/email/send?toEmail=recipient@gmail.com&subject=Hello&body=Test Email from Spring Boot

    After hitting this API, the email will be sent to recipient email address that you pass in the toEmail parameter.

    Testing Email Image

    Conclusion#

    Sending emails with Spring Boot is simple and production-ready with minimal setup. With the combination of Spring Boot Starter Mail and JavaMailSender, you can integrate email functionality into your application.

    Spring
    Spring Boot Send Email Tutorial
    JavaMailSender

    More articles