Grails Mail Plugin with DB Driven configuration

03 / Feb / 2011 by Vivek Krishna 2 comments

We have been coming across a certain use case in a couple of projects that the Email credentials/settings used for sending email should be different based on the from address. This is because the applications act as software services that are used by different organizations, which operate in the same business. And each needed their own customized emails and use their email credentials for sending of mails.

After searching a lot for such a solution, we found that such a use case has arisen for quite a few developers.

Another demand was that a mail service provider like GMail be used to send emails instead of using a mail server like Postfix of Sendmail.

What we came up was a modified version of the grails mail plugin, which works with DB driven mail configurations. It defaults to Config.groovy it it fails to fetch the MailConfig domain object based on the from address,  which we usually in the sendMail() method. The plugin makes use of a customMailSender bean, which is actually a copy of the mailSender bean which gets built from Config.groovy, but can be modified in a thread safe environment with values fetched from the DB.

The plugin is still in its infancy, but you can download the plugin and its source code here. You can read the documentation here.

Hope you find this useful



comments (2)

  1. Lari H

    Apache Camel is another possibility .

    (example from :
    Map map = new HashMap();
    map.put(“To”, “”);
    map.put(“From”, “”);
    map.put(“Subject”, “Camel rocks”);

    String body = “Hello Claus.nYes it does.nnRegards James.”;
    template.sendBodyAndHeaders(“smtp://”, body, map);

    You could also use in-memory queues (seda) to send in the background. There are tons of possibilities in Camel.

    Usage in Grails:
    grails install-plugin routing (

    BuildConfig.groovy changes for camel-mail support:
    mavenRepo “”
    in repositories.

    Add this to dependencies:
    runtime(“org.apache.camel:camel-mail:2.5.0”, “org.apache.camel:camel-test:2.5.0”) {
    excludes ‘spring-aop’, ‘spring-beans’, ‘spring-core’, ‘spring-expression’, ‘spring-asm’, ‘spring-tx’, ‘spring-context’, ‘spring-context-support’

    camel-test gives you TDD/Junit support.


Leave a Reply

Your email address will not be published. Required fields are marked *