[ 
https://issues.apache.org/jira/browse/EMAIL-211?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17935261#comment-17935261
 ] 

Hiran Chaudhuri commented on EMAIL-211:
---------------------------------------

JavaMail does not provide connection pooling, yet it is not necessary to 
deviate from using it. Just consider it on the level of a JDBC connection. If 
JDBC drivers were sufficient, how come projects like 
[https://commons.apache.org/proper/commons-dbcp/] were ever created?

So there is [https://commons.apache.org/proper/commons-pool/,] and all we'd 
need to do is
 * make commons-pool manage SMTP connections
 * make commons-email take advantage of pooled connections

If the server drops the connection after a short period of time (and there is 
no workaround like a keepalive) we cannot help it. But I am more focusing on 
the case where an application sends a burst of emails. In this situation enough 
connections would remain in use and only excess connections would be killed.

 

A JavaMail connection is established by initializing a session, then creating a 
Transport and calling it's connect method. The necessary parameters can all be 
passed in a properties object.

My idea was to create a factory method for Transport that takes Properties and 
decides internally which Transport to use. This would allow to not only create 
new required Transports but also to keep them for the next mail. To actually 
send the email I tried to make commons-email use my pooled/reused transport. 
But while Transport takes a MimeMessage, my calls to Email.getMessage() or 
Email.getMimeMessage() always returned null. It is unclear for me how to 
implement this at all - hence considering other libraries.

> Add connection pooling
> ----------------------
>
>                 Key: EMAIL-211
>                 URL: https://issues.apache.org/jira/browse/EMAIL-211
>             Project: Commons Email
>          Issue Type: New Feature
>            Reporter: Hiran Chaudhuri
>            Priority: Major
>
> Congratulations for an easy to use straightforward client library.
> However the application I created spends most of it's time connecting to the 
> mailserver. How come?
>  
> Apparently the examples on 
> [https://commons.apache.org/proper/commons-email/userguide.html] follow this 
> pattern:
>  
> {code:java}
> Email email = new SimpleEmail();
> ...
> email.addTo("[email protected]"); email.send();
> {code}
> As an effect, in the background it creates a JavaMail Session and Transport, 
> uses it to send the email and then forgets about it again. In my environment 
> building a connection requires to get an authentication token, then using 
> that for secure SMTP. Establishing such a connection takes a few seconds, 
> which limits the throughput of sending emails.
>  
> Please add a feature (or document how to do if already possible) to reuse 
> connections for sending several mails. Also the solution should be thread 
> safe so a multithreaded application can send emails in parallel.
>  
> I guess this should be easily doable by combining commons-pool and 
> commons-email. However my attempts failed miserably, making me want to switch 
> to other mail client libraries altogether.
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to