Streamlining Local SMTP Email Testing with MailTrap and Spring Boot

Manpreet Singh
2 min readMay 30

Local SMTP email testing is an essential part of software development to ensure the proper functioning of email notifications without sending them to real recipients. MailTrap provides a simulated SMTP server for testing email functionality. In this article, we will explore how to integrate MailTrap with a Spring Boot application to facilitate local SMTP email testing.

Prerequisites:

Before proceeding, ensure that you have the following prerequisites in place:

  1. Java Development Kit (JDK) installed on your machine.
  2. Spring Boot project setup.
  3. A MailTrap account. Sign up at https://mailtrap.io/ if you haven’t already.

Setting up MailTrap:

  1. Sign in to your MailTrap account.
  2. Create a new Inbox within MailTrap to simulate an email environment for testing.
  3. Take note of the SMTP settings provided by MailTrap, including the SMTP hostname, port number, username, and password.

Configuring Spring Boot Project:

  1. Open your Spring Boot project in your preferred IDE.
  2. Add the necessary dependencies to your project’s pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

3. Configure the MailTrap SMTP settings in the application.properties file:

spring.mail.host=your_mailtrap_smtp_hostname
spring.mail.port=2525
spring.mail.username=your_mailtrap_username
spring.mail.password=your_mailtrap_password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Implementing Spring Boot Code:

  1. Create a new Java class, such as EmailService, responsible for sending emails:
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 {

private final JavaMailSender mailSender;

@Autowired
public EmailService(JavaMailSender mailSender) {
this.mailSender = mailSender;
}

public void sendTestEmail() {
SimpleMailMessage message = new…
Manpreet Singh

Software developer who loves writing about programming, technology, passive income strategies etc.