java - Apache Commons Email and Reusing SMTP Connections -



java - Apache Commons Email and Reusing SMTP Connections -

currently using commons email send email messages, have not been able find way share smtp connections between emails sent. have code following:

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();

which readable, slow when big amount of messages, believe overhead of reconnecting each message. profiled next code , have found using reusing transport makes things 3 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 wondering if there way create commons email reuse smtp connection multiple email sendings? commons email api better, performance kind of painful.

thanks, ransom

i came next solution after digging commons source itself. should work, there may improve solutions not know of

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); email email = new simpleemail(); email.setfrom("example@example.com"); email.addto("example@example.com"); email.setsubject("hello example"); email.setmsg("hello example"); email.sethostname("localhost"); // buildmimemessage phone call below freaks out without // dug internals of commons email // send() buildmimemessage() + transport.send(message) // rather using transport, reuse 1 have email.buildmimemessage(); message m = email.getmimemessage(); transport.sendmessage(m, m.getallrecipients());

java email apache-commons-email

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -