I want to send thousands of different emails to different recipients and would like to open the connection to my SMTP and hold it. I hope this is faster then reopen the connection for ervy mail. I would like to use Apache Commons Email for that, but could fall back to the Java Mail API if necessary.
Right now I'am doing this, what opens a closes the connection every time:
HtmlEmail email = new HtmlEmail();
email.setHostName(server.getHostName());
email.setSmtpPort(server.getPort());
email.setAuthenticator(new DefaultAuthenticator(server.getUsername(), server.getPassword()));
email.setTLS(true);
email.setFrom("test@example.com");
email.addTo(to);
email.setSubject(subject);
email.setHtmlMsg(htmlMsg);
email.send();
You can use your earlier code but add the following to get the underlying Session
You can add extra java mail properties by
no need xtra code at all, just put your all email recipients and separate with comma.
MimeMessage pesan = new MimeMessage(session); pesan.setFrom(new InternetAddress("email_from@host.com")); pesan.setRecipients(Message.RecipientType.TO, InternetAddress.parseHeader("first_email@host.com,second_email@host.com,dst_email@host.com",false));
and do the same thing for
Message.RecipientType.CC
andMessage.RecipientType.BCC
if there is more than 1 email recipients hope its help :)..Here is my performance test class. Sending the mails using one connection is 4 times faster then reopen the connection every time (what happens when you use commons mail). The performance can be pushed further by using multiple threads.
Have a look at http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html. There is an example showing how to send an email. You should be able to send more before calling close() on the Transport.