Guide · Go, Rust & JVM
How to send email with Spring Boot
Send email from Spring Boot with spring-boot-starter-mail and JavaMailSender pointed at the Stack Emails SMTP relay.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
Add spring-boot-starter-mail, then configure application.properties:
spring.mail.host=smtp.stackemails.com
spring.mail.port=465
spring.mail.username=stackemails
spring.mail.password=${STACKEMAILS_API_KEY}
spring.mail.protocol=smtps
spring.mail.properties.mail.smtp.auth=true
2. Send your first email
import jakarta.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@Service
public class WelcomeMailer {
private final JavaMailSender mailer;
public WelcomeMailer(JavaMailSender mailer) { this.mailer = mailer; }
public void send(String to) throws Exception {
MimeMessage msg = mailer.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, "UTF-8");
helper.setFrom("hello@mail.acme.com", "Acme");
helper.setTo(to);
helper.setSubject("Welcome to Acme");
helper.setText("<p>Thanks for signing up!</p>", true);
mailer.send(msg);
}
}
A successful request returns 202 with the message id, for example { "id": "msg_5b1f0c6c2a8e4e0f", "status": "queued" }. Errors return a JSON body with error.code and error.message (see errors).
Production checklist
- Keep the API key in an environment variable or secret manager, never in client-side code.
- Add an
Idempotency-Keyheader to retries so a timeout never sends twice (idempotency). - Subscribe to webhooks for bounces and complaints.
- Check your domain's SPF, DKIM and DMARC records.
- Start from a tested email template for password resets, receipts and invites.