Saturday, January 28, 2012

Setting up email in a Grails application using Zoho Mail

Goodness, that was a hassle! I love Zoho's mail, having used it as the email provider for my business for the past 8 or 9 months. It's inexpensive (free if you want 3 or fewer email addresses) and their tech support is prompt when a problem crops up.

However, I just spent the longest time (well over an hour) trying to get SMTP set up for an application I am building for my church. Since I'm sure I'll end up using Zoho for mail processing in the future, I figured I'd capture the steps here for both my future self and anyone else who might benefit.

  1. Set up a Zoho account. I create a new email address for my main domain using a name that matches the application I'm setting up the account for: newapplicationname@davidcastro.com.
  2. Validate the account using the link that is emailed to newapplicationname@davidcastro.com.
  3. Update the DNS settings by adding the CNAME required to validate ownership of the domain.
  4. Confirm domain ownership using the Zoho interface.
  5. Update the DNS settings by adding an MX entry to point to 20 mx2.zohomail.com and 10 mx.zohomail.com.
  6. Create an email account for mydomain.com: email@mydomain.com in the Zoho interface.
  7. Configure email forwarding to my main email address.
  8. Validate the forwarding by clicking the link in the email sent to my main email address.
  9. Install the Mail plugin into my Grails application.
  10. In Config.groovy, add the following:
  11. grails {
      mail {
        host = "smtp.zoho.com"
        username = "username"
        password = "password"                    
        port = 465
        props = ["mail.smtp.auth":"true",                        
                 "mail.smtp.socketFactory.port":"465",
                 "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
                 "mail.smtp.socketFactory.fallback":"false"]
      }
    }
    
  12. In the method that triggers the email being sent, add the following:
  13. sendMail {
      to "email@domain.com"
      from "Application Name <email@mydomain.com>"
      subject "Subject line"
      body "Body of message"
    }
    
  14. And now we move onto the tricky part. We need to enable SMTP in the Zoho account. To do this:
    1.  In Zoho Mail, click the Settings link in the upper left corner.
    2. Click the Email forwarding an POP/IMAP link on the left panel.
    3. In the IMAP Access section of the right panel, change Status to "Enable."
  15. Next, we need to add the Send Mail As setting to the Zoho account so that it knows that you're not sending as an email address you don't own (spoofing). To do this:
    1. In Zoho Mail, click the Settings link in the upper left corner.
    2. Click the Send Mail as link on the left panel.
    3. In the top of the right panel, click Add From Address.
    4. Enter a display name, the email address to send from (email@mydomain.com), and then select the Zoho SMTP option.
    5. Click Send verification.
    6. Give it a couple of minutes and then retrieve the confirmation number in the email sent to email@mydomain.com and paste it into the Zoho dialog box that popped up after you clicked Send verification.