Apache Commons Email and SMTP Connection Reuse

I am currently using Commons Email to send emails, but I could not find a way to share smtp connections between sent emails, I have a code like:

Email email = new SimpleEmail(); email.setFrom(" example@example.com "); email.addTo(" example@example.com "); email.setSubject("Hello Example"); email.setMsg("Hello Example"); email.setSmtpPort(25); email.setHostName("localhost"); email.send(); 

This is very readable, but slow when I make a large number of messages, which I consider to be the overhead of reconnecting for each message. So I profiled it with the following code and found that using Transport reuse makes things about three times faster.

  Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); Session mailSession = Session.getDefaultInstance(props, null); Transport transport = mailSession.getTransport("smtp"); transport.connect("localhost", 25, null, null); MimeMessage message = new MimeMessage(mailSession); message.setFrom(new InternetAddress(" example@example.com ")); message.addRecipient(Message.RecipientType.TO, new InternetAddress(" example@example.com ")); message.setSubject("Hello Example"); message.setContent("Hello Example", "text/html; charset=ISO-8859-1"); transport.sendMessage(message, message.getAllRecipients()); 

So, I was wondering if there is a way to get Commons Email to reuse the SMTP connection for multiple emails? I like the Commons Email API better, but the performance is painful.

Thanks, Ransom

+6
source share
2 answers

I came up with the following solution after I dug up the source of commons itself. This should work, but there may be better solutions that I don't know about

  Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); Session mailSession = Session.getDefaultInstance(props, null); Transport transport = mailSession.getTransport("smtp"); transport.connect("localhost", 25, null, null); Email email = new SimpleEmail(); email.setFrom(" example@example.com "); email.addTo(" example@example.com "); email.setSubject("Hello Example"); email.setMsg("Hello Example"); email.setHostName("localhost"); // buildMimeMessage call below freaks out without this // dug into the internals of commons email // basically send() is buildMimeMessage() + Transport.send(message) // so rather than using Transport, reuse the one that I already have email.buildMimeMessage(); Message m = email.getMimeMessage(); transport.sendMessage(m, m.getAllRecipients()); 
+3
source

Could this be achieved more simply by receiving the mail session from the first email message using getMailSession () and placing it in all subsequent messages using setMailSession ()?

Not sure, that

Note that passing the username and password (in case of mail authentication) will create a new mail session using DefaultAuthenticator. This is a belief, but may seem unexpected. If mail authentication is used but there is no username and password, the implementation assumes that you have installed the authenticator and will use the existing mail session (as expected).

from javadoc tools though: - / http://commons.apache.org/email/api-release/org/apache/commons/mail/Email.html#setMailSession%28javax.mail.Session%29

see also: https://issues.apache.org/jira/browse/EMAIL-96

not sure how to proceed here ...

+1
source

Source: https://habr.com/ru/post/892756/


All Articles