# email Companion code for **Sending Emails in Spring Boot 4: SMTP, HTML Templates, Attachments and Testing** on [ankurm.com](https://ankurm.com/sending-emails-in-spring-boot-3-a-complete-guide/). 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 ```bash 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.