How to send Email notifications (user registered, import completed) using Spring

3.2k views Asked by At

I guess this is not a specific question, but getting ideas on how to.

We have a Flex - Spring - JMS - Hibernate webapp. Our requirement is to send automatic emails when some event occurs (new user registered, user performed an import successfully). We will have the Email addressess, content, Subject, etc in the DB. We can have the Mail Server settings hardcoded (or in the db). I would like to have this as a separate service for reusabulity. I would like to get your inputs on how to achieve this and also provide some optimal solutions on what would be the best way. Do I use JMS for this or Spring's Java Mail Sender. Which is more efficient and the latest.

Hope my question isn't too vague. Your inputs are appreciated as always :)

Thanks

Harry

1

There are 1 answers

4
Tomasz Nurkiewicz On BEST ANSWER

Here are few low-hanging choices you have (from best to worst):

  1. Spring Email support

    The Spring Framework provides a helpful utility library for sending email that shields the user from the specifics of the underlying mailing system and is responsible for low level resource handling on behalf of the client.

    This is the simplest approach (not counting coding to raw JavaMail API). If you need to just send that e-mail, you don't need nothing more. You can esaily inject JavaMailSender wherever you need it.

  2. Email support in

    Spring Integration provides support for outbound email with the MailSendingMessageHandler. It delegates to a configured instance of Spring's JavaMailSender [...]

    This solutions is built on top of 1. If you need a reusable service with enterprise integration patterns bundled (like filtering, routing, enhancing, etc.) and great flexibility. If your application does a lot of integration, this might be a smart choice but is a bit more heavyweight when it comes to maintenance.

  3. SMTPAppender in

    The SMTPAppender accumulates logging events [...] and sends the contents [...] in an email after a user-specified event occurs. SMTP email transmission (sending) is performed asynchronously.

    This Logback appender is typically used to send an e-mail yo support/administrator when an ERROR occurs, containing error event itself and few previous events. But with a little bit of configuration you can easily use to it catch specific events in your application (triggered by logging statements) and send messages. However it will be a bit cumbersome and not flexible enough.