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
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
== 1. SimpleMailMessage (sendPlain)
|
||||
text/plain; charset=UTF-8
|
||||
|
||||
== 2. HTML only, non-multipart helper (sendHtmlOnly)
|
||||
text/html;charset=UTF-8
|
||||
|
||||
== 3. welcome: text + HTML + inline logo + PDF attachment
|
||||
multipart/mixed
|
||||
multipart/related
|
||||
multipart/alternative
|
||||
text/plain; charset=UTF-8
|
||||
text/html;charset=UTF-8
|
||||
image/png; name=inline [inline; filename=inline] [Content-ID <logo>]
|
||||
application/pdf; name=welcome-guide.pdf [attachment; filename=welcome-guide.pdf]
|
||||
|
||||
== 4. welcome without inline image and without attachment (helper still in multipart mode)
|
||||
multipart/mixed
|
||||
multipart/related
|
||||
multipart/alternative
|
||||
text/plain; charset=UTF-8
|
||||
text/html;charset=UTF-8
|
||||
|
||||
== 5. addInline BEFORE setText
|
||||
multipart/mixed
|
||||
multipart/related
|
||||
image/png; name=inline [inline; filename=inline] [Content-ID <logo>]
|
||||
text/html;charset=UTF-8
|
||||
|
||||
== 6. addAttachment on a helper built with multipart=false
|
||||
IllegalStateException: Not in multipart mode - create an appropriate MimeMessageHelper via a constructor that takes a 'multipart' flag if you need to set alternative texts or add inline elements or attachments.
|
||||
@@ -0,0 +1,22 @@
|
||||
== template resolvers on the engine (class, order, resolvable patterns)
|
||||
ClassLoaderTemplateResolver order=0 patterns=[*.txt]
|
||||
SpringResourceTemplateResolver order=null patterns=[]
|
||||
|
||||
== welcome.html with name = Asha
|
||||
<h1>Welcome, <span>Asha</span></h1>
|
||||
|
||||
== welcome.txt with name = Asha (TEXT mode)
|
||||
Welcome, Asha
|
||||
|
||||
== name = <b>Evil</b> & Co
|
||||
th:text -> <h1>Welcome, <span><b>Evil</b> & Co</span></h1>
|
||||
th:utext -> <p><b>Evil</b> & Co</p>
|
||||
[(...)] in TEXT mode -> Welcome, <b>Evil</b> & Co
|
||||
[[...]] in TEXT mode -> Hello, <b>Evil</b> & Co
|
||||
|
||||
== a typo in a top-level variable name: ${nme} instead of ${name}
|
||||
<p>Hello, <span></span>!</p>
|
||||
|
||||
== a typo in a property of an object you did pass: ${user.nmae}
|
||||
TemplateProcessingException
|
||||
root cause: EL1008E: Property or field 'nmae' cannot be found on object of type 'com.ankurm.email.TemplateTest$User' - maybe not public or not valid?
|
||||
@@ -0,0 +1,11 @@
|
||||
== default encoding (spring.mail.default-encoding = UTF-8)
|
||||
raw Subject : =?UTF-8?B?4KS44KWN4KS14KS+4KSX4KSkIOCkueCliCwg4KSG4KS24KS+?=
|
||||
raw To : =?UTF-8?B?4KSG4KS24KS+IOCkpuClh+CktuCkruClgeCklg==?= <[email protected]>
|
||||
decoded : स्वागत है, आशा | To name: आशा देशमुख
|
||||
body CTE : base64
|
||||
body : आपका खाता तैयार है।
|
||||
|
||||
== a JavaMailSenderImpl whose defaultEncoding is ISO-8859-1
|
||||
raw Subject : =?ISO-8859-1?B?Pz8/Pz8/ID8/LCA/Pz8=?=
|
||||
decoded : ?????? ??, ???
|
||||
body : ???? ???? ????? ???
|
||||
@@ -0,0 +1,5 @@
|
||||
simulated SMTP latency: 50 ms per server reply; 5 messages
|
||||
send(message) once per message : 5 connections, 2083 ms
|
||||
send(message...) varargs : 1 connection, 1449 ms
|
||||
@Async sendWelcome x5 : caller resumed after 9 ms, all delivered after 444 ms, 5 connections
|
||||
ran on thread(s) : [task-3, task-4, task-5, task-6, task-7]
|
||||
@@ -0,0 +1,8 @@
|
||||
== void @Async, recipient 'not an address'
|
||||
caller: returned normally, no exception
|
||||
logged: [ERROR] Unexpected exception occurred invoking async method: public void com.ankurm.email.AsyncMailService.sendWelcomeFireAndForget(java.lang.String,java.lang.String) throws jakarta.mail.MessagingException
|
||||
exception: jakarta.mail.internet.AddressException
|
||||
|
||||
== CompletableFuture<String> @Async, same recipient
|
||||
future.get() threw ExecutionException
|
||||
cause: jakarta.mail.internet.AddressException: Local address contains control or whitespace
|
||||
@@ -0,0 +1,27 @@
|
||||
== wrong password
|
||||
org.springframework.mail.MailAuthenticationException: Authentication failed
|
||||
jakarta.mail.AuthenticationFailedException: 535 5.7.8 Authentication credentials invalid
|
||||
|
||||
== right password
|
||||
delivered: 1 message
|
||||
|
||||
== nothing listening on the port
|
||||
org.springframework.mail.MailSendException
|
||||
org.springframework.mail.MailSendException: Mail server connection failed. Failed messages: org.eclipse.angus.mail.util.MailConnectException: Couldn't connect to host, port: 127.0.0.1, 32877; timeout -1;
|
||||
org.eclipse.angus.mail.util.MailConnectException: Couldn't connect to host, port: 127.0.0.1, 32877; timeout -1
|
||||
java.net.ConnectException: Connection refused
|
||||
|
||||
== recipient that is not an address: 'not an address'
|
||||
org.springframework.mail.MailParseException
|
||||
org.springframework.mail.MailParseException: Could not parse mail
|
||||
jakarta.mail.internet.AddressException: Local address contains control or whitespace
|
||||
|
||||
== server accepts the TCP connection and never sends the SMTP greeting
|
||||
no timeouts set -> still blocked after 3000 ms; the caller is stuck
|
||||
(after the connection was dropped: MailSendException)
|
||||
mail.smtp.connectiontimeout=500 -> still blocked after 3000 ms; the caller is stuck
|
||||
(after the connection was dropped: MailSendException)
|
||||
mail.smtp.timeout=500 -> failed after ~500 ms
|
||||
org.springframework.mail.MailSendException: Mail server connection failed. Failed messages: jakarta.mail.MessagingException: Exception reading response;
|
||||
jakarta.mail.MessagingException: Exception reading response
|
||||
java.net.SocketTimeoutException: Read timed out
|
||||
@@ -0,0 +1,19 @@
|
||||
== spring.mail.host, port, username, default-encoding, ssl.enabled and two mail.* properties
|
||||
bean : JavaMailSenderImpl
|
||||
host / port : smtp.example.test / 2525
|
||||
protocol : smtp
|
||||
defaultEncoding : UTF-8
|
||||
session props : {mail.smtp.auth=true, mail.smtp.ssl.checkserveridentity=true, mail.smtp.ssl.enable=true, mail.smtp.timeout=7000}
|
||||
|
||||
== no spring.mail.host
|
||||
JavaMailSender bean present: false
|
||||
|
||||
== no spring.mail.host, and a bean that needs a JavaMailSender
|
||||
No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
|
||||
|
||||
== spring.mail.test-connection=true against a port nothing listens on
|
||||
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.mail.autoconfigure.MailSenderValidatorAutoConfiguration': Failed to instantiate [org.springframework.boot.mail.autoconfigure.MailSenderValidatorAutoConfiguration]: Constructor threw exception
|
||||
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.mail.autoconfigure.MailSenderValidatorAutoConfiguration]: Constructor threw exception
|
||||
java.lang.IllegalStateException: Mail server is not available
|
||||
org.eclipse.angus.mail.util.MailConnectException: Couldn't connect to host, port: 127.0.0.1, 1; timeout -1
|
||||
java.net.ConnectException: Connection refused
|
||||
@@ -0,0 +1,3 @@
|
||||
send inside the transaction, then roll back : 1 email delivered
|
||||
AFTER_COMMIT listener, then roll back : 0 emails delivered
|
||||
AFTER_COMMIT listener, commit : 1 email delivered
|
||||
@@ -0,0 +1,10 @@
|
||||
== mail.smtp.starttls.enable=true, server does not advertise STARTTLS
|
||||
delivered: 1 message, no error
|
||||
client sent: AUTH LOGIN
|
||||
client sent: YWxpY2U= (base64 of the user name: alice)
|
||||
client sent: czNjcmV0 (base64 of the password: s3cret)
|
||||
|
||||
== mail.smtp.starttls.required=true, same server
|
||||
org.springframework.mail.MailSendException: Mail server connection failed. Failed messages: jakarta.mail.MessagingException: STARTTLS is required but host does not support STARTTLS
|
||||
jakarta.mail.MessagingException: STARTTLS is required but host does not support STARTTLS
|
||||
AUTH line sent: false
|
||||
@@ -0,0 +1,12 @@
|
||||
APPLICATION FAILED TO START
|
||||
***************************
|
||||
|
||||
Description:
|
||||
|
||||
Parameter 0 of constructor in com.ankurm.email.MailService required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
|
||||
|
||||
|
||||
Action:
|
||||
|
||||
Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration.
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.013 s -- in com.ankurm.email.MimeStructureTest
|
||||
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.410 s -- in com.ankurm.email.TransactionTest
|
||||
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.727 s -- in com.ankurm.email.FailureModesTest
|
||||
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.164 s -- in com.ankurm.email.StartTlsTest
|
||||
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.389 s -- in com.ankurm.email.EncodingTest
|
||||
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.289 s -- in com.ankurm.email.AsyncAndConnectionsTest
|
||||
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.101 s -- in com.ankurm.email.AutoConfigurationTest
|
||||
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.167 s -- in com.ankurm.email.TemplateTest
|
||||
Tests run: 9, Failures: 0, Errors: 0, Skipped: 0
|
||||
Reference in New Issue
Block a user