Skip to content

JavaMailSender.doSend() should more aggressively catch exceptions [SPR-12298] #16904

@spring-projects-issues

Description

@spring-projects-issues

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:

  1. Catch Throwable from JavaMail instead of just MessagingException. Throw a MailSendException in all cases so the caller can determine which messages in the batch failed to send.
  2. Check SMTPTransport.isConnected() in the loop within doSend() and reconnect if the connection has been unexpectedly closed out from under us.

Affects: 4.1.1

Issue Links:

Referenced from: commits b7faef0

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions