grails.project.dependency.resolution = { ... plugins { ... compile ":mail:1.0.7" } }
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)
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?" } } }
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']) } } }
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 } } }