{"id":1050,"date":"2010-06-14T11:51:58","date_gmt":"2010-06-14T06:21:58","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1050"},"modified":"2016-12-19T15:04:56","modified_gmt":"2016-12-19T09:34:56","slug":"configuring-multiple-senders-in-grails-mail-plugin","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/configuring-multiple-senders-in-grails-mail-plugin\/","title":{"rendered":"Configuring multiple senders in Grails Mail plugin"},"content":{"rendered":"<p>In one of the recent projects, we had to set up a system where emails for different purposes (i.e registration, error reporting etc.) were to be sent out from different email accounts. We were using the Grails Mail plugin which does not provide a clear way to set-up or configure multiple email addresses. With the help of my colleague, <strong>Chandan Luthra<\/strong>, I tried to dig into the workings of plugin and found out that Mail plugin injects the properties such as username, password etc. through a bean named mailSender. So we just did the following to customize\/configure these properties on the fly from the controller or a service:<\/p>\n<pre>CH.config.grails.mail.default.from = \"username@domain.com\" \/\/ Mail plugin uses this to connect to the account in e-mail provider's server. So it should be set.\r\nmailSender.username = \"username@domain.com\"\r\nmailSender.password = \"sample123!\"<\/pre>\n<p>And yes, once you are done with your email sending functionality you can set mailSender properties back to the default settings set originally in Config.groovy. Your service\/controller code could look something like this:<\/p>\n<pre>\/\/ Store the default mail settings in variables\r\ndef defaultFrom = CH.config.grails.mail.default.from\r\nString oldUsername = mailSender.username\r\nString oldPassword = mailSender.password\r\n\r\n\/\/ Change the properties here; send the email\r\ntry {\r\nCH.config.grails.mail.default.from = \"username@domain.com\"\r\nmailSender.username = \"username@domain.com\"\r\nmailSender.password = \"sample123!\"\r\nmailService.sendMail {\r\nto(['exampple1@domain1.com', 'example2@domain2.com'].toArray())\r\nsubject(\"Example Subject\")\r\nbody(\"Sample body\")\r\n}\r\n}\r\ncatch (Exception e) {\r\n\/\/ catch block code\r\n}\r\n\/\/ Set the original settings back\r\nfinally {\r\nCH.config.grails.mail.default.from = defaultFrom\r\nmailSender.username = oldUsername\r\nmailSender.password = oldPassword\r\n}<\/pre>\n<p>I guess this scenario can be quite prevalent in many projects. Hope you find the blog useful.<\/p>\n<p>&#8211; Abhishek Tejpaul<\/p>\n<p>Intelligrape Software Pvt. Ltd.<\/p>\n<p>[<a href=\"http:\/\/www.tothenew.com\" target=\"_blank\">www.intelligrape.com<\/a>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of the recent projects, we had to set up a system where emails for different purposes (i.e registration, error reporting etc.) were to be sent out from different email accounts. We were using the Grails Mail plugin which does not provide a clear way to set-up or configure multiple email addresses. With the [&hellip;]<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":14},"categories":[7],"tags":[596,595,597,327],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1050"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1050"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1050\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}