-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Description
Eric Sirianni opened SPR-12298 and commented
My application is hitting an issue whereby the MS Exchange server is closing the SMTP connection after 10 minutes of use. This manifests itself in an IllegalStateException
from javamail's SMTPTransport
object:
java.lang.IllegalStateException: Not connected
at com.sun.mail.smtp.SMTPTransport.checkConnected(SMTPTransport.java:2354)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1151)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:424)
The error handling from Spring's JavaMailSender
is not ideal in this case. My application code is sending messages in batch and was prepared to catch Spring's MailSendException
to determine which emails in the batch were send successfully vs. which failed to send. However, the underlying IllegalStateException
is bubbling up from JavaMail, leaving my application code no way to determine which messages were actually sent successfully.
Two possible enhancements would make JavaMailSender
batch sending more robust:
- Catch
Throwable
from JavaMail instead of justMessagingException
. Throw aMailSendException
in all cases so the caller can determine which messages in the batch failed to send. - Check
SMTPTransport.isConnected()
in the loop withindoSend()
and reconnect if the connection has been unexpectedly closed out from under us.
Affects: 4.1.1
Issue Links:
- JavaMailSenderImpl - Set empty String params to null for setUsername and setPassword [SPR-12294] #16899 JavaMailSenderImpl - Set empty String params to null for setUsername and setPassword
Referenced from: commits b7faef0