BBackend CourseLeroy Labs
0/0
Search the course

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.

● intermediateHostel-Web-AppReport-Manager-2IR3D (Kontentt)SperoTek (in dev)

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

Hostel-Web-AppEmailService · templates/(student-verification, password-reset, staff-activation, manager-activation).html

Gmail 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.

Report-Manager-2shared/config/SesMailConfig.java · templates/email/*.html (12) · StagingSafetyTest

Gmail 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.

SperoTek (in dev)SmtpEmailTransport · resources/mail/* · _layout.html · Mailpit

This is the most polished setup:

  • Each template has three files, an HTML body, a plain-text body and a subject, and they share _layout.html and _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.
IR3D (Kontentt)EmailServiceImpl · EmailSender · EmailBranding · templates/email/fragments/footer.html

Branding 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.

  • 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, not th:utext, for user-provided values.
  • Localisation. Thymeleaf message bundles handle multi-language email.

Check yourself

1. Which DNS record cryptographically signs outgoing mail?
2. Why does SperoTek send a .txt version too?
3. Why disable the mail health indicator?