Sending e-mails with Grails: the simple way

Sending e-mails with Grails: the simple way 1 Sending e-mails with Grails can be quite complicated (yeap, I know about the Mail Plugin, but I also know many people who had some problems with it AND it’s a Grails only alternative).

If you don´t want to use a plugin, you can always count with the Java Mail API, or even the Spring Framework support for it, right? Yeah, right: but at least for me, it seems like a to complex solution for a simple need. So, I started my quest for a simple way to do it. The solution I found was right in front of me all the time (so usual…): Commons E-mail, which is part of the Apache Commons project!

To show how you can use it, here is a small snippet:


import org.apache.commons.mail.SimpleEmail
class MailService {

boolean transactional = false
// I added the configuration on the same class just for the sake of simplicity (you should NEVER do this on a real case)
String host= "your.mail.ver"
String username = "your.login"
String password = "your.password"
String from = "me@me.com"
Integer port = 465

def send(String subject, String msg, String to) {

//SimpleEmail is the class which will do all the hard work for you
SimpleEmail email = new SimpleEmail()
email.setHostName(host)
email.addTo(to)
email.setFrom(from)
email.setSubject(subject)
email.setMsg(msg)

//If you need authentication, this is the method
email.setAuthentication(username,password)

// If you need to specify your smtp port, this is how to
email.setSmtpPort(port)

// So all you need to do is call the method send() and your e-mail will be delivered (at least in theory)
email.send()
}
}

Just like I wanted: simple!

Dependencies

All you need is JAF, which may be downloaded on the following link: http://java.sun.com/javase/technologies/desktop/javabeans/jaf/index.jsp

Where to download Commons E-mail: http://commons.apache.org/email/

9 comments on “Sending e-mails with Grails: the simple way

  1. Matt Passell

    To make the code even more “Groovy”, you could take advantage of “with” (see Getting Groovy With “with”) and not have to repeat your references to the email variable (comments removed for the sake of brevity):

    SimpleEmail email = new SimpleEmail()
    email.with {
    setHostName(host)
    addTo(to)
    setFrom(from)
    setSubject(subject)
    setMsg(msg)
    setAuthentication(username,password)
    setSmtpPort(port)
    send()
    }

    Responda

    admin Reply:

    Yeap! Looks great like this!

    But in this case, I think that a more “Java” approach is enough (after all, it was one of my requisites :) )

    Responda

  2. Ron Barlag

    I’m looking for a means to batch process mail in gmail using Groovy.
    Of course there is the javamail api, but do you know something similar like the a apache commons simple mail?

    Responda

  3. ofer

    great post 1 comment you have to put javax.mail.jar at the lib directory
    and also make the project know about this jar other wise it is compile but at run time
    you get error

    Responda

    admin Reply:

    Thanks.

    Yes, that’s right. You have to add the jar files of the JavaMail API on your grails-app/lib folder too.

    Responda

  4. devika

    Hey thanks a lot for the code above… it was very simple… Just have one problem… I am not able to use this smtp server : smtp.conwerge.mail… It is however working just fine with gmail… Please let me know your comments

    Responda

  5. Matt

    Great tutorial! I did 2 things:
    1) add commons-email-1.2.jar and mail.jar from Oracle to the lib folder
    2) add the dependency build ‘org.apache.commons:commons-email:1.2’ to BuildConfig.groovy
    => works like a champ on my local hMail server – even with grails 2.1.0.
    Thank you very much!

    Responda

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.