Grails Cookbook - A collection of tutorials and examples

Grails Mail Plugin Tutorial

This tutorial will show how to send email messages from your controllers or services using the Grails mail plugin. The plugin is very easy to use as it could talk to SMTP servers. You can connect to your own mail servers or to other popular mail providers such as Gmail or Yahoo.

Overview

The plugin needs to connect to an SMTP server to send an email. For example, if you have a Gmail account, you can configure the plugin to use it. The plugin will connect to the Gmail account via SMTP protocol by providing your username and password. After sending messages using the plugin, you will be able to view them in the Sent Folder when logged to the browser client.

Install Mail Plugin

The first step is to install the plugin by editing grails-app/conf/BuildConfig.groovy. Add entry similar to below in the plugins section:
grails.project.dependency.resolution = {
    ...
    plugins {
        ...
        compile ":mail:1.0.7"
    }
}

Configure Mail Plugin

Edit grails-app/config/Config.groovy and add an entry for the mail plugin.

If you wish to use Gmail, add something similar to this:

grails {
    mail {
        host = "smtp.gmail.com"
        port = 465
        username = "myusername@gmail.com"
        password = "mypassword"
        props = ["mail.smtp.auth":"true",
                "mail.smtp.socketFactory.port":"465",
                "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
                "mail.smtp.socketFactory.fallback":"false"]
    }
}


A better approach is to use JNDI so that the application's WAR is not bound to hard-coded email configuration. You can define the email details in the application server. If you are using Tomcat7 (this may work in other versions of Tomcat), you can add the following entry in context.xml

<Context>
	<Resource name="mailSession"
		auth="Container"
		type="javax.mail.Session"
		mail.debug="false"
		mail.hostname="smtp.gmail.com"
		mail.smtp.user="myusername@gmail.com"
		password="mypassword"
		mail.transport.protocol="smtp"
		mail.smtp.host="smtp.gmail.com"
		mail.smtp.auth="true"
		mail.smtp.starttls.enable="true"
		mail.smtp.EnableSSL.enable="true"
		mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
		mail.smtp.socketFactory.fallback="false"
		mail.smtp.port="465"
		mail.smtp.socketFactory.port="465"                
    />
</Context>

And this is how you can wire it in your Config.groovy

grails.mail.jndiName = "java:comp/env/mailSession"

But there is no JNDI when doing grails run-app. So it is a good idea to combine the two approach above. Use a hard-coded approach for development environment and use JNDI for production:

environments {
    development {
        grails {
            mail {
                host = "smtp.gmail.com"
                port = 465
                username = "myusername@gmail.com"
                password = "mypassword"
                props = ["mail.smtp.auth":"true",
                        "mail.smtp.socketFactory.port":"465",
                        "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
                        "mail.smtp.socketFactory.fallback":"false"]
            }
        }
    }
    production {
        grails.mail.jndiName = "java:comp/env/mailSession"
    }
}

If you are having a difficult time running the application due to jar conflicts, move the following files from the WAR to the lib folder of Tomcat: (note: remove these jars from the WAR so that there is only one copy, which should be in the lib folder of Tomcat)
  • activation-1.1.1.jar
  • javax.mail-1.5.1.jar
  • javax.mail-api-1.5.1.jar

Sending Emails from Controller/Service

The sendMail method is injected into controllers and services. You can send email inside controllers like this:

class TestController {
    def testAction() {
        sendMail {
            to "myfriend@gmail.com"
            subject "This is a test mail"
            body "Hello, This is a test mail, how are you?"
        }
    }
}

Sending an email from a service is also similar:

class TestService {
    def testMethod {
        sendMail {
            to "myfriend@gmail.com"
            subject "This is a test mail"
            body "Hello, This is a test mail, how are you?"
        }
    }
}

Sending Emails with GSP Body

You may compose your email in GSP. For example, create the file grails-app/views/mail/_test.gsp

<h3>Greetings ${name}!</h3>
<p>Welcome to our Service</p>
<p>The quick brown fox jumps over the lazy dog.</p>

And use it like this:

class TestController {
    def testAction() {
        sendMail {
            to "myfriend@gmail.com"
            subject "This is a test mail"
            html g.render(template:'/mail/test', model:[name:'John Doe'])
        }
    }
}

Sending Emails with Attachment

When sending an email, multipart true is required. The parameter attachBytes defines the attached file. The parameters are: file name when file is downloaded, mime type of file, byte array of the file contents.

class TestController {
    def testAction() {
        sendMail {
            multipart true
            to "myfriend@gmail.com"
            subject "This is a test mail"
            body 'Check the attachmet'
            attachBytes "test.zip", " application/x-compressed", new File('c:/temp/test.zip').bytes
        }
    }
}


Other Grails Plugins Tutorials


Tags: mail, Plugin