Wednesday, 7 January 2015

Grails : How to send a mail


Step 1 : create account in https://mandrillapp.com/login/

Mandrill is a scalable and affordable email infrastructure service, with all the marketing-friendly analytics tools you’ve come to expect from MailChimp.

step 2 : goto settings > smtp & API info

Step 3 : select API key. it generates one api key

step 4 : Add compile ":mail:1.0.7" in BuildConfig.groovy at plugins

step 5 : In config.groovy, place below mail settings

grails {
mail {
 host = "smtp.mandrillapp.com"
 port = 587
 username = "your@mail.com"
 password = "apikey"
 props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"587"]
 
}
}

In above code, mandrill username & api key

Step 6 : In your controller inject mail services like shown in below

def mailService

def yourAction(){

mailService.sendMail {
multipart true
//to "sender1@mail.com","sender2@mail.com"
//to "sender@mail.com"
from "support@mail.com"
subject "Subject"
body 'body content'
//attachBytes "test.zip", " application/x-compressed", new File('D:/delete/test.zip').bytes
attachBytes 'new year.jpg','image/jpg', new File('D:/delete/new year.jpg').readBytes()
html  g.render( template: '/admission/mailTemplate')
//attachBytes 'Penguins.jpg','image/jpg', new File('D:/delete/Penguins.jpg').readBytes()
//attachBytes 'Penguins.jpg','image/jpg', new File('D:/delete/Penguins.jpg').readBytes()
 
  }
}

Step 7 : clean your project

Step 8 : run the application

Step 9 : call yourAction and then mail will be send.

Thank U.. Happy Coding..

No comments:

Post a Comment