Spring Boot

Spring Boot's mail starter reads five properties — point them at a sandbox.

Before you start

  • Spring Boot 2.7 or later
  • spring-boot-starter-mail

1Add the starter

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

2Configure the mail sender

application.properties
spring.mail.host=smtp.mailclutch.com
spring.mail.port=587
spring.mail.username=mc_0000000000000000
spring.mail.password=pw_0000000000000000
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Put it in application-staging.properties if your production profile has its own mail server.

3Send a test message

@Autowired
private JavaMailSender mailSender;

public void sendTest() {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("staging@example.com");
    message.setTo("anyone@example.com");
    message.setSubject("Sandbox check");
    message.setText("Hello from Spring Boot.");

    mailSender.send(message);
}

Watch it land

Open the sandbox and the message is there — rendered HTML, plain text, raw source, attachments, and the SPF, DKIM and DMARC verdicts. It updates live, so you can leave it open next to your terminal.

Nothing is ever delivered onward, whatever address you send to; claim the inbox with an account and it stays.

Awaiting your email

Send from any address; it stops here rather than going onward. The inbox keeps what it catches for 24 hours.

What usually goes wrong

  • @Async swallows the send

    A mail method annotated @Async fails on another thread — the exception never reaches your request. Send synchronously while you are checking the setup.

  • YAML instead

    The same keys nest under spring: mail: in application.yml; the SMTP extras go under properties: mail.smtp.*.

Official documentationSpring Boot — Sending Email (opens in a new tab)