Files
spring-boot-demo/email/README.md
T
Claude da700a564f Add email module: JavaMailSender on Spring Boot 4.1.2 / Jakarta Mail 2.1.5, tested against GreenMail with real transcripts
Nine tests, each writing a transcript to email/output/: the MIME tree for six message shapes (mixed > related >
alternative; addInline before setText puts the image first; addAttachment on a non-multipart helper throws),
Thymeleaf HTML and TEXT templates ([[...]] still HTML-escapes in TEXT mode, [(...)] does not), Devanagari subjects as
encoded-words and a wrong defaultEncoding turning them into '?', connections and latency through a delaying TCP proxy
(5 connections for one send per message, 1 for the varargs send, @Async returning in milliseconds), a void @Async
failure that never reaches the caller, a server that never sends the SMTP greeting blocking forever until
mail.smtp.timeout is set (connectiontimeout does not help), starttls.enable authenticating over plain text with the
base64 password captured on the wire while starttls.required refuses, spring.mail.* mapped onto JavaMailSenderImpl,
spring.mail.test-connection, and an email still sent when the transaction rolls back unless it waits for AFTER_COMMIT.

Found while writing: GreenMailExtension starts per test method by default, so a @DynamicPropertySource that reads its
port needs withPerMethodLifecycle(false).

No docs/ folder; the explanations live in the article's accordions.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b7SK2mvUEELeLtMi1o
2026-11-18 21:17:37 +05:30

3.4 KiB

email

Companion code for Sending Emails in Spring Boot 4: SMTP, HTML Templates, Attachments and Testing on ankurm.com.

One MailService builds each message shape (plain text, HTML, a Thymeleaf HTML + text pair with an inline image and a PDF attachment) and sends it through Spring Boot's auto-configured JavaMailSender. The tests send to a GreenMail server running inside the JVM, read the result back, and write what they saw to output/. A small TCP proxy in front of GreenMail adds latency and counts connections; a "black hole" server that never sends the SMTP greeting shows what happens without timeouts.

Piece Version Checked against
Spring Boot 4.1.2 maven-metadata.xml for spring-boot-starter-parent (4.2.0-RC2 exists but is not GA)
Spring Framework 7.0.10 mvn dependency:tree
Jakarta Mail API / Angus Mail 2.1.5 / 2.0.5 (Angus is runtime scope) mvn dependency:tree
Thymeleaf 3.1.5.RELEASE (managed by Boot) mvn dependency:tree
GreenMail 2.1.15 maven-metadata.xml
JDK 25 Temurin 25.0.5+8

Run it

export JAVA_HOME=/path/to/jdk-25
mvn -B test              # 9 tests, transcripts 01-09 written to output/
./scripts/run-all.sh     # the above plus transcript 10 (the app without spring.mail.host) and the test summary

Nothing else is needed: no SMTP account, no Docker. To send real mail, set spring.mail.host, port, username and password (see src/main/resources/application.yml; MAIL_USERNAME and MAIL_PASSWORD are read from the environment) and call MailService from your own code. There is no web layer in this module.

What is in output/

File What it shows
01-mime-structure.txt the MIME tree for six ways of building a message, including addInline before setText and addAttachment on a non-multipart helper
02-templates.txt the two Thymeleaf resolvers, th:text vs th:utext, [(...)] vs [[...]] in TEXT mode, a silent typo in a variable name vs a loud one in a property
03-encoding.txt a Devanagari subject and display name as encoded-words on the wire, and a wrong defaultEncoding turning them into ?
04-async-and-connections.txt connections and time for one send per message, one varargs send, and @Async, with 50 ms of simulated SMTP latency (timings are indicative)
05-async-failures.txt a void @Async failure that never reaches the caller, and the same failure through a CompletableFuture
06-failure-modes.txt wrong password, closed port, bad address, and a server that never greets: blocked forever without mail.smtp.timeout (the port in this file changes per run)
07-autoconfiguration.txt what spring.mail.* becomes on JavaMailSenderImpl, no JavaMailSender without a host, spring.mail.test-connection
08-transactions.txt an email sent inside a transaction that rolls back, and the AFTER_COMMIT fix
09-starttls.txt starttls.enable authenticating over plain text (the base64 password captured on the wire) vs starttls.required refusing
10-no-mail-host.txt the console output when the app starts without spring.mail.host
11-test-summary.txt the Maven test summary

Transcripts 01-09 come out of the test suite, so each number in them is an assertion that fails the build when it stops being true. Transcript 04's timings depend on the machine.