Grails: Sending email using Ant with GMail SMTP server

SMTP is one service I don’t want to burden my little site with. So I’m trying to send emails using the Gmail SMTP server.

Actually, sending emails with Grails is a breeze. There are different ways but I find that using the Ant mail task is the most effortless one. Thanks to the excellent demonstration by Glen Smith with the GroovyBlogs code, I could get started in a fraction of time. But to successfully use the GMail SMTP service, there are a few things to pay attention to.

1. Be sure to copy necessary jars to your grails-app/lib

The required jars include:

*If you are on Java 1.5 or earlier, you need the additional activation.jar. Since I’m using Java 6, it’s included in the JDK and I don’t need a copy in my lib.

2. GMail SMTP settings

This has literally cost me some hours so I hope it’s helpful to you. I at first searched for a set working settings and it turns out port 587 is the working one (for Rails). I did so but hit into mysterious exceptions with SSL.

: Problem while sending mime mail:
at groovy.util.AntBuilder.nodeCompleted(AntBuilder.java:199)
...
Caused by: javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
...

This lead me to the wrong direction and I spent some time investigating if there’s anything wrong with my config and Ant dealing with SMTP behind SSL/TLS. But finally, it turns out that port 587 was NOT being friendly to Ant over SSL. Use port 465 and it’s all good! (which is the opposite situation with Rails)

A little summary

So the complete config for GMail looks like this. (As written in Config.groovy)

mail{
  host="smtp.gmail.com"
  port="465"
  ssl="on"
  user="your.addr@gmail.com"
  password="password"
}

Use these as the params for the Ant mail task. To glue things together with Grails, you just need to

  1. Put the Ant params in Config.groovy so that they can be easily modified later.
  2. Create a service class for sending emails. e.g. MailService.groovy. Then in the service method, use AntBuilder’s DSL to create the mail task. It should look something like
    import org.codehaus.groovy.grails.commons.ConfigurationHolder as C
    ...
    new AntBuilder().mail(
      mailhost:C.config.mail.host,
      mailport:C.config.mail.port,
      ssl:C.config.mail.ssl,
      user:C.config.mail.user,
      password:C.config.mail.password,
      subject:msgSubject
    ){
      from(address:C.config.mail.reply.name)
      to(address:toAddr)
      message(mimetype:"text/html", content)
    }

That’s pretty neat, eh? If you feel like getting a peek at some working code, you can grab the GroovyBlogs source and do the modifications to talk with GMail in minutes.

Tags: , , , ,

  • There's a typo (a missing 'c') in line 6: It needs to be ssl:C.config.mail.ssl

    Thanks for your blogpost, I never thought implementing the mailservice could be done in less than five minutes ... working immediately :)
  • Oh yeah, sorry about that! I've corrected it now. Glad this could be helpful to you!:))
  • Nice guide, thanks!
  • simppler
    Thank you very much! Really helpful!
  • cooky
    That's really helpful.
    Thanks!
    ;)
  • Hi! I'm Jake Bunce, the manager of Viettel ISP, in my opinion you've make a nice writing. You are on my RSS reader now so I can read more from you down the road. I have just given it a Digg :D, Thanks for your awesome post.
  • Thank - great tip using port 465. I never would have figured that out.
  • Thanks man, just what I was looking for. Worked like a charm Thanks so much…
  • >>2. GMail SMTP settings
    Thanks a lot for the 465 fix! Helped a lot while I was setting up Hudson :)
blog comments powered by Disqus