java - How to send multiple emails in one session? -
i want send thousands of different emails different recipients , open connection smtp , hold it. hope faster reopen connection ervy mail. use apache commons email that, fall java mail api if necessary.
right i'am doing this, opens closes 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();
here performance test class. sending mails using 1 connection 4 times faster reopen connection every time (what happens when use commons mail). performance can pushed further using multiple threads.
properties properties = system.getproperties(); properties.put("mail.smtp.host", server); properties.put("mail.smtp.port", "" + port); session session = session.getinstance(properties); transport transport = session.gettransport("smtp"); transport.connect(server, username, password); (int = 0; < count; i++) { message message = new mimemessage(session); message.setfrom(new internetaddress(from)); internetaddress[] address = {new internetaddress(to)}; message.setrecipients(message.recipienttype.to, address); message.setsubject(subject + "javamail api"); message.setsentdate(new date()); sethtmlcontent(message); message.savechanges(); transport.sendmessage(message, address); } transport.close();
Comments
Post a Comment