My web app sends emails to users who are logged in. It always uses same setup:
mail.smtp.host=…
mail.smtp.user=…
mail.smtp.password=…
I am confused whether I should for every message do connect-send-close:
t.connect(host, username, password);
t.sendMessage(...);
t.close();
…or should I connect only once and just keep on sending different messages:
t.connect(host, username, password);
t.sendMessage(...);
t.sendMessage(...);
t.sendMessage(...);
t.sendMessage(...);
t.close();
…since the host, username, password don’t change.
What’s right thing to do?
Sending multiple messages per connection will be more efficient, but you need to be prepared to handle failures at any point in the process, including connection failures. That may make your error handling code more complex.