From the JavaMail FAQ, this is the optimal way of sending bulk mail:
MimeMessage msg = ...;
// construct message
msg.saveChanges();
Transport t = session.getTransport("smtp");
t.connect();
for (int i = 0; .....) {
t.sendMessage(msg, new Address[] { recipients[i] });
}
t.close();
However, to create a session, we need to do something like so:
Session session = Session.getInstance(properties);
This means the properties need to be set before the session is created. This can be problematic if I'm using something like VERP, where the "From" property will different for each recipient. So, is there a way of setting the properties for a Session dynamically, while still reusing the same Transport object?
It's not defined by the spec, but the "mail.smtp.from" property is read from the Properties object each time you send a message, so you can change the property in the Properties object used when the Session was created and it will see the change.
If that seems a bit too "obscure", you can use an SMTPMessage object instead of a MimeMessage object and call the setEnvelopeFrom method for each recipient.