Stella Peristeraki
Stella Peristeraki

Reputation: 289

Recommended way to send email from a web app?

I have a web app on JBoss 4.2.3 and I'd like it to send email. I could do something like:

try {
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp" );
props.put("mail.smtp.starttls.enable","false" );
props.put("mail.smtp.host","smtp.somehost.com");
props.put("mail.smtp.auth", "true" );
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress("[email protected]"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]", false));
msg.setSubject("yadayada");
msg.setText("Yada yada");
// -- Set some other header information --
msg.setHeader("MyMail", "Mr. XYZ" );
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
}
catch (Exception ex) {
  ex.printStackTrace();
  System.out.println("Exception "+ex);
}

but it doesn't feel right. Will this scale?

Upvotes: 1

Views: 1447

Answers (2)

Brian Agnew
Brian Agnew

Reputation: 272277

How many messages are you sending ? Have you measured how long the above takes to run ? (I'm assuming that the major time sink will be in the actual send() to the MTA. Whether that's significant is another matter)

Perhaps:

  1. you need to queue the messages for sending to your MTA in a queue, and have the sending running in a separate thread ?
  2. you need an appropriate mailing list / alias set up, and thus only send one mail for 'n' recipients ?

but all that hinges on how many messages you're going to send, and how distinct the sets of recipients are.

Upvotes: 1

Chris
Chris

Reputation: 15641

With Spring use the Spring Mail Abstraction Layer

Upvotes: 1

Related Questions