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
source share