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!
Excellent!
Using the Spring Mail API this way, is there any possibility to get the id of the email sent? Amazon generates these ids and in a (hope near) future, we will be able to track the message info with it.
Is there any problem to link this blog post to the Grails AWS Plugin page? People really need to know this possibility instead of just the pure SES API.
Thanks,
Comment by Lucas Teixeira — April 6, 2011 @ 11:56 pm
Thanks, Lucas.
No, there is no problem at all in getting this blogpost linked with Grails AWS Plugin page. Please go ahead and link it. I will be happy about it.
I am pretty sure, at this point, any email id returned by SES must be getting lost while returning through Spring mail api and Mail plugin, but will try to have a look around it tomorrow anyway.
Comment by roshandawrani — April 7, 2011 @ 12:11 am
Thanks Roshan!
Comment by Lucas Teixeira — April 7, 2011 @ 9:44 am
Just added this blog post under the FaQ section.
Best regards
George
Comment by George — January 2, 2012 @ 7:47 pm
Nice blog, I had an error though after setting everything up. I also needed to add the stax jar in my dependencies to fix the exception I was getting – javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.MXParserFactory not found
i.e.
dependencies {
runtime ‘com.amazonaws:aws-java-sdk:1.1.9′
runtime ‘stax:stax:1.2.0′
}
Comment by Matt G — April 14, 2011 @ 3:19 am
Thank you.
Yes, I had got the stax dependency error too.
It seems while stax-api was a regular dependency, stax was an optional dependency of aws-java-sdk and didn’t get pulled-in. We ended up excluding stax-api dependency, as it didn’t seem needed for the mail functionality we were using.
Comment by roshandawrani — April 14, 2011 @ 8:05 am
Great, but there is one problem… Why do I have to setup my AWS credentials in one another place (grails.mail.props slurp) if I had configured them before in grails.plugin.aws.credentials closure?
Comment by Yuriy Yarovoy — September 16, 2011 @ 8:41 am
Thanks, Yuriy. The technique discussed in the blogpost is absolutely independent of Grails AWS plugin. I wanted to try and make it work with just standard Grails Mail plugin – that was the main attraction of the experiment
Comment by roshandawrani — September 16, 2011 @ 8:55 am
OK, I understood. Thanks!
Comment by Yuriy Yarovoy — September 17, 2011 @ 11:32 pm
Any way to have the AWS credentials in a .properties file as per the Grails AWS plugin. I have just switched to the mail plugin so I can send attachments, I am waiting for either one of them to support S3 at rest encrypted files.
Comment by stevef — November 11, 2011 @ 4:18 am