Grails: Using Mail Plugin With Amazon Email Service

This is a quick note on how to use Grails Mail plugin with Amazon’s Simple Email Service – an inexpensive, hassle-free, reliable, scalable mail-sending solution. It’s not meant to cover any details about Amazon SES itself, because those are pretty well documented, but here are some links to get started:

It is assumed from here on that you have registered with Amazon Web Services and have valid credentials to login with.

Grails Mail plugin is based on Spring Mail support and by default uses “smtp” as the mailing protocol. With SMTP, it can be configured as below:

grails {
    mail {
        host = '<some host>'
        port = <some port>
        username = 'foo@example.org'
        password = 'xxxxxxx'
        props = ['mail.smtp.auth': 'true', ...]
    }
}

However, when you want to use the Mail plugin with Amazon SES, you need to switch the protocol to “aws” and specify the transport implementation class, as shown in the configuration below:

grails {
    mail {
        props = [ 
            'mail.transport.protocol': 'aws', 
            'mail.aws.class': 'com.amazonaws.services.simpleemail.AWSJavaMailTransport',
            'mail.aws.user': '<your aws access key>',
            'mail.aws.password': '<your aws secret key>' 
        ]
    }
}

While above configuration change is the primarily required one, there are 2 more little changes that need to be made to switch to SES:

a) While the above configuration change registers “aws” protocol with its implementation transport class, you need to explicitly tell Mail plugin again that you want to use “aws” as the protocol in your application

grails.mail.protocol = 'aws'

b) This second bit is needed because if you don’t provide any mail host, the mail plugin takes “localhost” as the default, which confuses AWS mail API because it thinks that you are trying to override the default endpoint of the SES mail service by another specific endpoint (host). So, it needs to be explicitly cleared to let SES API use its default endpoint (https://email.us-east-1.amazonaws.com)

beans {
    mailSender.host = ''
}

That’s all!

Just point your Grails app to AWS SDK on maven repository, make the above configuration changes and start using Mail plugin to send mails through your Amazon Simple Email Service account and enjoy the hassle-free mailing from your Grails application!