grails install-plugin mail
Grails Mail Plugin integration is very simple - everything You need is to dynamically inject mail service into Your service. After that You can use its sendMail method for sending email:
Do not forget to configure Your mail server, configuration is located in grails-app/Config.groovy file. In my example it can be similar to listed below:class MyService {
// Dynamic injection happens here.
MailService mailService
def someMethod() {
// Some logic.
mailService.sendMail {
to "email@mail.com"
from "youraccount@gmail.com"
subject "Test mail"
html """Hello!
This is a test email. """
}
}
}
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "youraccount@gmail.com"
password = "yourpassword"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
One topis is not covered - unit and integration testing of mail service. You will not believe but only thing You need is to initialise mail service in Your integration or unit test and connent it to my service. Short example:
That's all concerning Grails Mail Plugin, hope You enjoyed it.class MyServiceTests extends GrailsUnitTestCase {
MyService myService
void setUp() {
// Do not forget to call super method at first.
super.setUp()
// Initialize the my service.
userService = new UserService()
// Initialize the mail service and
// connect it to my service
userService.mailService = new MailService()
}
// Some tests.
}
Hi, I installed the plugin but I'm getting the following error,
ReplyDeleteGroovy:unable to resolve class MailService
Do I need to import some jars in the classpath?
A little late... but maybe this will help someone... You have to import grails.plugin.mail..
ReplyDeleteHow do we config for MS Exchange Server?
ReplyDelete