Digital Marketing

Difference between the Transport methods send and sendMessage

Refer to Example of sending Email from Gmail by Java for more infomation.

The send() method is a static method and can be used without needing an instance of a Transport object. It is intended for the common, simple case of sending a single message using the default transport. Internally, the send() method will first call the saveChanges() method on the message. It will then create an appropriate new Transport object, call the Transport's connect()method, call the Transport's sendMessage() method to actually send the message, call the Transport's close() method, and finally abandon the new instance of the Transport object to be collected by the garbage collector. The static send() convenience method is built on the more general per-instance sendMessage() method. If your application just sends some emails, using
is more convenient. Here send is a static method that creates and manages its own connection. Any connection associated with any Transport instance used to invoke this method is ignored and not used. This method should only be invoked using the form Transport.send(msg);, and should never be invoked using an instance variable.

If your application sends a lot of email then to improve performance, you should use the sendMessage() method directly.
Properties props = new Properties();
props.put("", host);
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
Transport t = null;
try {
Transport t = session.getTransport("smtp");
t.sendMessage(msg, msg.getAllRecipients());
} finally {
if (t != null) {
Unlike the static send method, the sendMessage method does not call the saveChanges method on the message; the caller should do so.

Tested on both Amazon SES and for 32 emails. sendMessage() is about 2 times faster than send().


Popular posts from this blog