June 16, 2009
This is the 10th article in the Getting Started with Grails tutorial series. The entire series is as follows:
- Introduction
- Getting started with JRuby
- Getting started with GlassFish
- Restarting GlassFish
- Getting started with load balancing (Apache)
- Load balancing with web server redundancy (Apache)
- Load balancing with web server failover (Apache)
- Getting Started with Message Oriented Web Services (Java EE)
- Setting up a local network using FireWire
- Sending Email from Grails
- Deploying Grails on GlassFish v3
Generating a pleasingly formatted email based upon information from a database and delivering it via an industry standard SMTP mail server is a cornerstone towards realizing a Web 2.0 application. The implementation of this capability however seems to be mired in opinion, quickly changing software and sometimes just plain confusion. This tutorial is my attempt at rewriting the chapter on implementing email services in Grails (pages 269-274) from the book Beginning Groovy and Grails: From Novice to Professional. As such, the code provided within this article builds upon the collab-todo web application presented within chapters 1 through 8 of this book.

This how-to article will culminate with you being able to send an HTML formatted email from your Grails application to your Gmail account that will look something like:

As mentioned within the previous Adventures (but worth mentioning again) this Tutorial is based upon my current system configuration which is a MacBook Pro laptop running Mac OS X Version 10.5.6 (Leopard), NetBeans 6.7 RC1, Groovy 1.6.3 and Grails 1.1.1
![]()
Step 1 – Configure Grails with the Email Plugin…
Installing plug-ins into your Grails application is done through an internet enabled download service. This install procedure is instigated via a Grails command line instruction. Within NetBeans, these powerful command line instructions can be accessed through the “Run Grails Command” window:

Select the “install-plugin” task and enter “mail” within the Parameters textbox:

Upon clicking the
button, you’ll see a bunch of messages culminating with:

![]()
Step 2 – Reference the Email Plugin…
Insert the following try clause into the UserController’s save action as follows:

Since the above code is hard to read, the following code is provided (from which you can cut/paste if you wish).
try {
sendMail {
to “${userInstance.emailAddress}”
subject “Collab-todo Registration Confirmation”
html g.render(template:’/email/registrationConfirmation’, model:[user:userInstance])
}
flash.message = “Confirmation email sent to ${userInstance.emailAddress}”
} catch(Exception e) {
log.error “Problem sending email $e.message”, e
flash.message = “Confirmation email NOT sent”
}
The above code references the sendMail object which was supplied within the mail plugin.
![]()
Step 3 – Embellish the User with an emailAddress Property…
The above code also references the “emailAddress” property which has not yet been added to the User Domain Class. Therefore, open the Domain Class and add the emailAddress declaration and constraint line (two added lines of code total) as shown below:

And save it to disk.
![]()
Step 4 – Establish the registrationConfirmation Template…
The sendMail code that we inserted into the UserController’s save action also makes reference to a template called “/email/registrationConfirmation”. This is created in a couple of steps. First create the common directory within which all of your email templates can reside called “email” as follows:

Enter the text “email” into the Folder Name textbox:

And then click the
button.
Now generate a new gsp file that will reside within this newly created email directory:

When the New GSP File wizzard window appears, enter “_registrationConfirmation” into the File Name textbox. It’s important that this name is prefixed with an underscore (indicating that it is a Grails template).

Upon clicking the
button, you’ll see the auto-generated code within your new GSP file:

Although the above looks like a good HTML skeleton, we only need to represent what is contained within the HTML body. Therefore replace all of the auto-generated code with the following single line:

For readability, the above line of HTML code is repeated below:
<center><h1>Hello ${user.firstName}<p>Welcome to collab-todo.</h1></center>
![]()
Step 5 – Configure sendMail…
Within the project’s Configuration directory, there exists a “Config.groovy” file which, as you would expect, configures default parameters to items within your Grails app. If this configuration file is not modified as described below, you’ll get an email error message stating that contacting the SMTP server located at localhost port 25 failed. Open up “Config.groovy”:

And append the following grails parameter map:

For clarity, here’s a more readable form of the code:
// sendMail configuration…
grails {
mail {
host = “smtp.gmail.com”
port = 465
username = “yourGmailAccountName@gmail.com”
password = “yourGmailPassword”
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
Of course, you’ll want to replace the “yourGmailAccoutName” and “yourGmailPassword” with ones specific to your Gmail account. The above default assignment statements will configure your Grails sendMail object to connect to and authenticate into a Gmail SMTP server.
![]()
Step 6 – Update the User scaffolding…
Being that we have introduced a new emailAddress parameter into the User class, we’ll either need to regenerate the static User views or delete the static user views and revert back to using dynamic views for the User class. I chose the latter:

![]()
Step 7 – Run the Application…
Just to be on the safe side, I like to clean out the intermediate and generated Grails directories through the use of the “clean” command:

Which causes the following “run” command to regenerate everything from scratch:

Upon rebuilding the project, your default web browser will be invoked and directed to your Grails web-app. Surfing over to your User list view, you’ll notice that the emailAddress is now included.

Upon clicking the
button, you’ll be presented with the Create User view. Enter your User Name, First Name, Last Name and of course the Email Address.

Upon clicking the
button, you’ll notice that the Show User view is now displaying a note indicating that: “Confirmation email sent to <yourEmailAddress>”:

![]()
Step 8 – Admire the email sent from your Grails Web App…
And now for the moment that you’ve been waiting for… Tan ta da daaaaa… your first Grails-generated email:

Whoo-Hoo00! Congratulations. :-)
June 16, 2009 at 10:00 pm |
[...] Started with Grails Fletcher Mcbeth « Introductory Grails Project using NetBeans Sending Email from Grails [...]
June 16, 2009 at 10:45 pm |
[...] Sending Email from Grails [...]
December 8, 2009 at 11:26 pm |
Hi, been trying to get the grails mail plugin to work with a GSP template but could not work out where to put the GSP. Your post helped sort this.
Thanks
Ayub
April 13, 2010 at 5:40 pm |
Thanx very much.Your post is easy to understand and takes it work!!! Greate job.:)
July 26, 2010 at 12:35 pm |
would you please give us the how to of upload file in to grails :) cause every one else can’t describe it as you do man you are really the best :)