Send Mail via Log4j with SMTP Appender

13 / Aug / 2012 by Amit Kumar 6 comments

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:

// import SMTPAppender and Log4j Lever classes
import org.apache.log4j.net.SMTPAppender
import org.apache.log4j.Level

...

//  Mail server Configration
mail.error.server = 'smtp.gmail.com'
mail.error.port = 587
mail.error.username = 'senderEmailId@gmail.com'
mail.error.password = 'senderPassword'
mail.error.to = 'receiver@gmail.com'
mail.error.subject = 'Mail Subject'
mail.error.starttls = true
mail.error.debug = false

....

// log4j Configration.
log4j = {
     System.setProperty 'mail.smtp.starttls.enable', config.mail.error.starttls.toString()
     System.setProperty 'mail.smtp.port', config.mail.error.port.toString()

     // To send all errors or bigger level messages in email via SMTPAppender
     appender new SMTPAppender(name: 'smtp', to: config.mail.error.to,
          from: config.mail.error.from, subject: config.mail.error.subject, threshold: Level.ERROR, SMTPHost: config.mail.error.server,
          SMTPUsername: config.mail.error.username, SMTPDebug: config.mail.error.debug.toString(),
          SMTPPassword: config.mail.error.password,
          layout: pattern(conversionPattern:'%d{[ dd.MM.yyyy HH:mm:ss.SSS]} [%t] %n%-5p %n%c %n%C %n %x %n %m%n'))

....

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

NOTE: If you are using a version below Grails2 then you will have to use mail.error.port instead of config.mail.error.port and mail.error.server instead of config.mail.error.server in log4j block, etc.

2. Add dependency for mail.jar and activation.jar.
3. Add log.error(“Error Message”), when you want to mail error messages.

Amit Kumar

FOUND THIS USEFUL? SHARE IT

comments (6)

  1. Chanda Hassanein

    Very interesting information!Perfect just what I was looking for! “It’s not the having, its the getting.” by Elizabeth Taylor.

    Reply
  2. Nazeel

    when i tried this, i get error
    | Error log4j:ERROR Error initializing log4j: javax/mail/Message
    | Error java.lang.NoClassDefFoundError: javax/mail/Message

    Reply
  3. Una

    It’s going to be ending of mine day, except before finish I am reading this wonderful
    article to improve my knowledge.

    my blog post: Garcinia Cambogia; Una,

    Reply
  4. Ralph

    It’s iin fact very complex in this full oof activity life to listen news on TV,
    thus I simply use web for that purpose, and obtain the most up-to-date information.

    Reply
  5. Charu Jain

    This will send mail synchronously, so sending a mail synchronously will lead to a *HUGE* performance hit, so is their any other way to send mail asynchronously?

    Reply
  6. Travis

    I love your blog.. very nice colors & theme.
    Did you design this website yourself or did you hire someone to do it for you?
    Plz reply as I’m looking to construct my own blog and would like to find out where u got this from. cheers

    Feel free to visit my blog – Travis

    Reply

Leave a comment -