{"id":6115,"date":"2012-08-13T19:52:48","date_gmt":"2012-08-13T14:22:48","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6115"},"modified":"2015-07-09T14:52:03","modified_gmt":"2015-07-09T09:22:03","slug":"send-mail-via-log4j-with-smtp-appender","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/send-mail-via-log4j-with-smtp-appender\/","title":{"rendered":"Send Mail via Log4j with SMTP Appender"},"content":{"rendered":"<p>Grails supports sending all log messages(error,fatal etc.) to a <a title=\"Grails Programing Team\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">support team<\/a> or to yourself, by <strong>email (gmail account),<\/strong> via <strong>log4j<\/strong> with <strong>SMTP Appender<\/strong>.<\/p>\n<p>1. For this we have to do some configration in<strong> Config.groovy<\/strong> as shown below:<\/p>\n<p>[groovy]<br \/>\n\/\/ import SMTPAppender and Log4j Lever classes<br \/>\nimport org.apache.log4j.net.SMTPAppender<br \/>\nimport org.apache.log4j.Level<\/p>\n<p>&#8230;<\/p>\n<p>\/\/  Mail server Configration<br \/>\nmail.error.server = &#8216;smtp.gmail.com&#8217;<br \/>\nmail.error.port = 587<br \/>\nmail.error.username = &#8216;senderEmailId@gmail.com&#8217;<br \/>\nmail.error.password = &#8216;senderPassword&#8217;<br \/>\nmail.error.to = &#8216;receiver@gmail.com&#8217;<br \/>\nmail.error.subject = &#8216;Mail Subject&#8217;<br \/>\nmail.error.starttls = true<br \/>\nmail.error.debug = false<\/p>\n<p>&#8230;.<\/p>\n<p>\/\/ log4j Configration.<br \/>\nlog4j = {<br \/>\n     System.setProperty &#8216;mail.smtp.starttls.enable&#8217;, config.mail.error.starttls.toString()<br \/>\n     System.setProperty &#8216;mail.smtp.port&#8217;, config.mail.error.port.toString()<\/p>\n<p>     \/\/ To send all errors or bigger level messages in email via SMTPAppender<br \/>\n     appender new SMTPAppender(name: &#8216;smtp&#8217;, to: config.mail.error.to,<br \/>\n          from: config.mail.error.from, subject: config.mail.error.subject, threshold: Level.ERROR, SMTPHost: config.mail.error.server,<br \/>\n          SMTPUsername: config.mail.error.username, SMTPDebug: config.mail.error.debug.toString(),<br \/>\n          SMTPPassword: config.mail.error.password,<br \/>\n          layout: pattern(conversionPattern:&#8217;%d{[ dd.MM.yyyy HH:mm:ss.SSS]} [%t] %n%-5p %n%c %n%C %n %x %n %m%n&#8217;))<\/p>\n<p>&#8230;.<\/p>\n<p>     root {<br \/>\n          error &#8216;stdout&#8217;, &#8216;smtp&#8217;<br \/>\n          additivity = false<br \/>\n     }<br \/>\n}<\/p>\n<p>[\/groovy]<\/p>\n<p><strong>NOTE:<\/strong> If you are using a version below <strong>Grails2<\/strong> then you will have to use <strong>mail.error.port<\/strong> instead of <strong>config.mail.error.port<\/strong> and <strong>mail.error.server<\/strong> instead of <strong>config.mail.error.server<\/strong> in <strong>log4j<\/strong> block, etc.<\/p>\n<p>2. Add dependency for <strong>mail.jar<\/strong> and <strong>activation.jar<\/strong>.<br \/>\n3. Add log.error(&#8220;Error Message&#8221;), when you want to mail error messages.<\/p>\n<p>Amit Kumar<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grails supports sending all log messages(error,fatal etc.) to a support team or to yourself, by email (gmail account), via log4j with SMTP Appender. 1. For this we have to do some configration in Config.groovy as shown below: [groovy] \/\/ import SMTPAppender and Log4j Lever classes import org.apache.log4j.net.SMTPAppender import org.apache.log4j.Level &#8230; \/\/ Mail server Configration mail.error.server [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":32},"categories":[7],"tags":[471,595,861],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6115"}],"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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6115"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6115\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}