Search This Blog

Friday, December 24, 2010

Grails template driven emailing with mail plugin


In this post I will walk through how to configure email using mail plugin.
1. Install mail plugin in your project.
grails install-plugin mail
2.
Add activation and mail jars to lib directory.
I use these jars
activation:1.1.1
mail-1.4.3
3.
To resources.groovy add mailSender definition and configure it
    mailSender(org.springframework.mail.javamail.JavaMailSenderImpl) {
        host = 'smtp.gmail.com'
        port = 465
        username = 'xxx@gmail.com'
        password = 'xxx'
        javaMailProperties = ['mail.smtp.auth': 'true',
                'mail.smtp.socketFactory.port': '465',
                'mail.smtp.socketFactory.class': 'javax.net.ssl.SSLSocketFactory',
                'mail.smtp.socketFactory.fallback': 'false']
    }
    
    
4. Add a email template(if needed)
Create a directory grails-app\views\emailtemplates
Add a email.gsp with code you want -sample below
<%@ page contentType="text/html" %>
<html>
<head><title>${mytitle}</title></head>
<body><h1>${mymail}</h1></body>
</html>
ContentType of the gsp should be changed based on your need.
5. In your controller add the emailing code
           mailService.sendMail{
        to    "xxx@yahoo.com"
                from  "xxx@gmail.com"
                subject  "Email test"
            body  (
                view: "/emailtemplates/email",
                model: [mytitle: "My title", mymail: "Test Email"]
            )
            }
            
     If you dont need a template to render from just supply the body of the email.

No comments:

Post a Comment