Sending email through amazon mail service in Grails

Posted By : Kundan Ray Akela | 28-Jun-2015

Sending email through amazon mail service in Grails

In this blog , I am going to share information about how to configure and send email through Amazon simple email sevice.

A short details about Amzon SES.
We need to send email like business messages of transactional messages in almost every application and we cannot ignore the complexity and cost.Managing our sending email properly we must need to deal with some challeges like infrastructure(server management,network configuration, and IP address reputation).

Amazon SES properly handle these challenges.We can use SMTP or API call to get Amzon high quality email services.

Steps to configure email:

1.Obviously , we need to signup on Amazon (https://portal.aws.amazon.com) 

2.We need to verify that you own the domain or address from which you’ll be sending email.

3.You can increase the limit of sending email.

4.We can use either SMTP or the Amazon SES API to queue an email message for delivery.

5.Amazon SES provides useful statistics about our sending activities.

6.All we need to do Amazon SES console.

Sending in grails:

Build Config

compile 'com.amazonaws:aws-java-sdk:1.9.37'

In config

grails.plugin.awssdk.accessKey='YOUR_AWS_ACCESS_KEY'
grails.plugin.awssdk.secretKey='YOUR_AWS_SECRET_KEY'
gralis.plugin.awssdk.ses.sender='YOUR_SENDER_ADDRESS'

Here is a simple service that sends email.

package com.makzy.service
import com.amazonaws.services.simpleemail.model.Body
import com.amazonaws.services.simpleemail.model.Content
import com.amazonaws.services.simpleemail.model.Destination
import com.amazonaws.services.simpleemail.model.Message
import com.amazonaws.services.simpleemail.model.SendEmailRequest
import com.makzy.domain.User

class MailService {
    def grailsApplication

    /** The transactional. */
    static transactional = false

    /** The amazon web service. */
    def amazonWebService

    /** The groovy page renderer. */
    def groovyPageRenderer

    /**
     * Sets the async mail.
     *
     * @param model - domain object
     * @param template - template used for mail
     * @param from - from address
     * @param destination - array of addresses mail is sent to
     * @param subject - mail subject
     * @return the java.lang. object
     */
    public def sendMail(subject, template, model, destination) {

        def from = grailsApplication.config.gralis.plugin.awssdk.ses.sender
        def output = groovyPageRenderer.render view: '/emailtemplates/' + template, model: [data: model]
        Body body = new Body().withHtml(new Content(output.toString()))
        Message message = new Message(new Content(subject), body)
        def result = amazonWebService.ses.sendEmail(new SendEmailRequest(from.toString(), new Destination([destination]), message))
	println result
	return result
    }
}

Here emailtemplate is a html page containing content of the mail.

Request for Proposal

Recaptcha is required.

Sending message..