Transactional email with SMTP, SES, Thymeleaf templates & deliverability
JavaMailSender, HTML + text templates with Thymeleaf, Gmail vs Amazon SES, SPF/DKIM/DMARC, staging safety, Mailpit for local testing and bounce handling.
Sending
@Service @RequiredArgsConstructor
class EmailService {
private final JavaMailSender mail;
private final SpringTemplateEngine templates;
void send(String to, String template, Map<String, Object> vars, String subject) throws MessagingException {
Context ctx = new Context(Locale.ENGLISH, vars);
String html = templates.process("email/" + template, ctx); // templates/email/x.html
MimeMessage msg = mail.createMimeMessage();
var h = new MimeMessageHelper(msg, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, "UTF-8");
h.setTo(to); h.setSubject(subject); h.setFrom("no-reply@yourdomain.com");
h.setText(plainTextVersion, html); // always send a text alternative
h.addInline("logo", new ClassPathResource("static/logo.png"));
mail.send(msg);
}
}
spring.mail:
host: email-smtp.eu-west-1.amazonaws.com
port: 587
username: ${SES_SMTP_USER}
password: ${SES_SMTP_PASS}
properties.mail.smtp: { auth: true, starttls.enable: true, connectiontimeout: 10000, timeout: 10000, writetimeout: 10000 }
Always set timeouts. By default, JavaMail can hang forever on a dead SMTP server and tie up a thread.
In your projects
EmailService · templates/(student-verification, password-reset, staff-activation, manager-activation).htmlGmail SMTP on 465 (SSL), @Async, MULTIPART_MODE_MIXED_RELATED for inline images. The mail health indicator is disabled, so an SMTP outage doesn’t mark the whole app unhealthy and get it restarted.
shared/config/SesMailConfig.java · templates/email/*.html (12) · StagingSafetyTestGmail or Amazon SES, switched by whether the SES SMTP variables are set. There are 12 templates (account-created, password-reset, report-submission-*, reminders, two-factor). In staging, every email is redirected to one inbox.
SmtpEmailTransport · resources/mail/* · _layout.html · MailpitThis is the most polished setup:
- Each template has three files, an HTML body, a plain-text body and a subject, and they share
_layout.htmland_parts.html. - Templates: invitation, mfa-code, reset-password, verify-email, welcome, account-locked, roles-changed.
- 10-second SMTP timeouts.
- Mailpit catches mail locally (UI on :8025) and is used in integration tests, so tests assert that the email actually arrived with the right link.
- Sending goes through an encrypted outbox.
EmailServiceImpl · EmailSender · EmailBranding · templates/email/fragments/footer.htmlBranding is centralised, the footer is a Thymeleaf fragment, and email is queued through RabbitMQ (EmailDispatchListener).
Deliverability
| Record | Purpose |
|---|---|
| SPF (TXT) | Lists which servers may send for your domain |
| DKIM | A cryptographic signature on each email; your provider gives you DNS keys |
| DMARC | Policy for failures (p=quarantine) plus reporting |
| Custom MAIL FROM / return-path | Alignment for DMARC |
Without these, your password resets go to spam. Gmail SMTP is fine for prototypes, but it has a daily sending limit, and “from” addresses on your own domain need extra setup. Move to SES, Postmark, Resend or SendGrid for production.
Bounces and complaints
Providers send bounce and complaint notifications (SES via SNS). Stop sending to hard-bounced addresses, or your sender reputation drops.
Related concepts you must know
- Transactional vs marketing email. Use separate streams or subdomains so marketing complaints don’t hurt password resets.
- Unsubscribe headers (
List-Unsubscribe) are required by Gmail and Yahoo for bulk senders. - Template injection. Use
th:text, notth:utext, for user-provided values. - Localisation. Thymeleaf message bundles handle multi-language email.