Skip to content

Enhancement: auto-reconnect (if needed) when reclaiming a Transport connection from the SMTP connection pool (to avoid needless errors) #430

Closed
@lopardo

Description

@lopardo

Hi, first I'd like to thank the authors of this library, it's just what I needed and the API is awesome.

I have an email notification service that uses 2 different SMTP servers depending on some rules (so they're independent, I have 2 separate Mailer instances).

They sometimes need to send a bunch of emails at the same time, so I'd like to reuse the connections as much as possible to improve performance.

After testing my Mailers with a 60 second expiration time for the connection pool like this:

MailerBuilder.withSMTPServer(host, port, username, password)
    .withConnectionPoolExpireAfterMillis(60000)

One of the SMTP servers is OK with that, but the other seems to allow about 30 seconds before disconnecting the client. I know most servers only allow a few seconds before dropping the connection, but I'd like to take advantage of the first server.

This is the exception I get when the connection times out:

org.simplejavamail.mailer.internal.MailerException: Failed to send email [<211055192.7.1672161843409@host.lan>], reason: Third party error
	at org.simplejavamail.mailer.internal.SendMailClosure.handleException(SendMailClosure.java:97)
	at org.simplejavamail.mailer.internal.SendMailClosure.executeClosure(SendMailClosure.java:89)
	at org.simplejavamail.mailer.internal.AbstractProxyServerSyncingClosure.run(AbstractProxyServerSyncingClosure.java:56)
	at org.simplejavamail.mailer.internal.MailerImpl.sendMail(MailerImpl.java:345)
	at org.simplejavamail.mailer.internal.MailerImpl.sendMail(MailerImpl.java:331)

Caused by: com.sun.mail.smtp.SMTPSendFailedException: 421 Timeout - closing connection
	at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2374)
	at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1808)
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1285)
	at org.simplejavamail.mailer.internal.util.TransportRunner.lambda$sendMessage$0(TransportRunner.java:48)
	at org.simplejavamail.mailer.internal.util.TransportRunner.sendUsingConnectionPool(TransportRunner.java:78)
	at org.simplejavamail.mailer.internal.util.TransportRunner.runOnSessionTransport(TransportRunner.java:64)
	at org.simplejavamail.mailer.internal.util.TransportRunner.sendMessage(TransportRunner.java:47)
	at org.simplejavamail.mailer.internal.SendMailClosure.executeClosure(SendMailClosure.java:84)

I guess I could check the cause and retry myself, but maybe a setting could be added to automatically check if the connection is still valid before attempting to send an email?

Like adding at TransportRunner.java:48 :

if (!transport.isConnected()) {
    transport.connect();
}

My previous quick and dirty implementation using JavaMail 1.6 directly reused a single Session/SmtpTransport instance (pretty much like this example: https://stackoverflow.com/a/30932969), checking transport.isConnected() each time before attempting to send an email and that seemed to work OK (it only reconnected as necessary), but obviously it was limited to a single thread.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions