Here is a simple method to stay alert of any error being generated on your server. Basically we are going to use SMTP appender. I did a simple google search and found out various blogs but nothing seemed to work in Grails 2.2.0 that’s the version i am using. After a lot of google search and reading java forums i found out the configuration which works for me. Here it goes :

System.setProperty 'mail.smtp.starttls.enable', 'true'  
System.setProperty 'mail.smtp.starttls.required', 'true'  
System.setProperty 'mail.smtp.EnableSSL.enable', 'true'  
System.setProperty 'mail.smtp.auth', 'true'  
System.setProperty 'mail.smtp.socketFactory.port', '465'  
System.setProperty 'mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory'  
System.setProperty 'mail.smtp.socketFactory.fallback', 'false'

The following properties were missing from my initial config “mail.smtp.starttls.enable” , “mail.smtp.starttls.required” and “mail.smtp.auth”

Now we will move on to setting the properties needed by appender. These can be directly put into the appender but it’s always good to make things configurable.

     
mail.error.to = '<FirstEmail>'
mail.error.cc = '<email1>,<email2>'     
mail.error.smtp.username = '<your full gmail address>'    //This email account will be used to send emails     
mail.error.smtp.password  = '***********'    //Password of the email account     
mail.error.subject = 'Error : Exception generated on server'     
mail.error.smtp.host = 'smtp.gmail.com'
mail.error.smtp.debug = true     
mail.error.smtp.port = 465     

Now we will move on to configuring the actual SMTP appender needed by our application to send the email alerts to us.

     

log4j = {
	appenders {
		appender new SMTPAppender(name: 'smtp', to: config.mail.error.to,from: config.mail.error.smtp.username, 
		cc: config.mail.error.cc,subject: config.mail.error.subject, threshold: Level.ERROR,SMTPHost: config.mail.error.smtp.host,
		SMTPUsername: config.mail.error.smtp.username,SMTPDebug: config.mail.error.smtp.debug, 
		SMTPPassword: config.mail.error.smtp.password,sendOnClose: true, SMTPPort: 465,
		
		layout: pattern(conversionPattern:'%d{[ dd.MM.yyyy HH:mm:ss.SSS]} [%t] %n%-5p %n%c %n%C %n %x %n %m%n'))}

		error 'org.codehaus.groovy.grails.web.servlet',  //  controllers     
		      'org.codehaus.groovy.grails.web.pages', //  GSP
		      'org.codehaus.groovy.grails.web.sitemesh', //  layouts
		      'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping 
		      'org.codehaus.groovy.grails.web.mapping', // URL mapping
		      'org.codehaus.groovy.grails.commons', // core classloading
		      'org.codehaus.groovy.grails.plugins', // plugins
		      'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
		      'org.springframework',
		      'org.hibernate',
		      'net.sf.ehcache.hibernate'

		warn 'org.mortbay.log'

			root { 
				error 'stdout', 'smtp'
				additivity = true
			}     
		}     
	}

If you know a better way please feel free to share with us. Hope it helps.

PS: You have to install mail plugin to get this working.