I have recently experienced a performance issue in a module that is coded to send email notifications to multiple recipients using Spring Java Mail support. The email text for each recipient was different and hence it was initially coded to loop through all recipients, prepare
While reading through the Javadoc of Spring classes, I found that there is another send method in JavaMailSender class which takes an array of
Under the covers this method could be using the same mail session to send all
MimeMessage
with the help of an anonymous inner class of MimeMessagePreparator for each recipient, and call JavaMailSender
's send(MimeMessagePreparator mimeMessagePreparator) method. For every message the send method was taking about 20 secs or so for the call to finish. When there about 20 recipients, there were 20 such calls to this method and the delay was noticeable in minutes.While reading through the Javadoc of Spring classes, I found that there is another send method in JavaMailSender class which takes an array of
MimeMessage
s as an argument and sends mail in batch mode. I used this method instead and prepared an array of MimeMessage
s one per each recipient using just the MimeMessageHelper. This improved the performance and for 20 recipients which is 20 emails anyway, it only took about 20 seconds.Under the covers this method could be using the same mail session to send all
MimeMessage
s in batch mode.