Press ESC to close

Log4j Gmail SmtpAppender, send log emails via Gmail on Errors !

Log4j gives SMTPAppender for sending emails on logging. This SMTP Appender works good in  most of the cases, except TLS/SSL SMTP setups. Gmail works on the same SSL/TLS setup, so any one who wants to configure the default log4j SMTPAppender with Gmail will be troubled with strange Java Mail errors.

Though this problem can be easily solved by writing a Log4j Custom SMTP Appender.

“GmailSMTPAppender“ – Custom SMTPAppender for GMAIL

“GmailSMTPAppender” is the solution to the default log4j SMTPAppender SSL/TLS issue. GmailSMTPAppender is basically an extension to existing log4j SMTPAppender. It just overrides two methods from SMTPAppender i.e.

  1. protected Session createSession()

  2. protected void sendBuffer()

So by this inheritance GmailSMTPAppender utilizes the log4j’s SMTPAppender for most of the logic and specializes few methods for Gmail specific handling.

How to Install GmailSmtpAppender in my project ?

GmailSMTPAppender is open source, here is the project landing page [link]. Following are the steps to install this appender to your project.

  • Checkout the project source from here. This source is checking of a full Eclipse “Java project”.

  • Copy to your source folder.

  • Only dependencies are log4j-1.2.16.jar and java mail.jar. You can replace them with latest version if required or use your existing ones.

  • Copy and replace available here as required. You have to change the usual SMTPAppender settings for “log4j.appender.EMAIL”. This is explained in detail below.

  • Thats it, all done !!. Use Logger.error() to send email notifications.

What about Google APPS Account ?

GmailSmtpAppender works fine with both normal Gmail accounts and Google apps account. The same SMTP Host will go for Google apps accounts. So just update the SMTPUser/Pass with From/To addresses and you are done.

Source Code & DEPENDENCIES ?

The complete code + dependencies is hosted on GoogleCode. You can checkout the code from here.

Setting up

Here is the sample You can change or copy stuff like appenders from it as required. GmailSMTPAppender has not exposed any new appender settings. So just use your usual SMTPAppender settings and they will work fine with GmailSMTPAppender too.

#Google Code Project
#My Blog :

log4j.rootLogger=ERROR, Console,R, Console, R, EMAIL

log4j.appender.Console.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

# Turn off debugging if not required
log4j.appender.EMAIL.Subject=Email Notification from Gmail SMTP Appender
log4j.appender.EMAIL.layout.ConversionPattern=%p %t %c - %m%n

Sample ERROR Emails !

log4j error email in gmail inbox
Log4j error email body


Let me know in case of issues.

Comments (9)

  • Anonymoussays:

    June 15, 2011 at 11:59 am

    Hello Abhinav,I want to use your appender in a Google App Engine application, but I want to use a Windows Live email ID to send/receive the log messages.Can I do this using your appender? Or are some changes required?Regards,Arvind.

  • Anonymoussays:

    June 15, 2011 at 6:21 pm

    Hey Arvind,I believe, you should give a first shot with default SMTP Appender, if it doesn't works, then try creating a clone of GMailSMTPAppender to address windows live specific issues.THanks,Abhinav

  • Anonymoussays:

    July 27, 2011 at 2:57 pm

    Hello Abhinav,I am trying to integrate your appender + log4j into a java web app.I copied the file into correct folder, and also added the dependencies for log4j and mail JAR files.However in Netbeans I am getting the following errors in–(1) For class GmailSMTPAppender– Duplicate class.(2) In code segment– “return new PasswordAuthentication(getSMTPUsername(),getSMTPPassword());” I am getting the following errors- Cannot find symbol:method getSMTPUsername and another error – Cannot find symbol:method getSMTPPasswordAre these errors occurring because I am using these in a web application? Or is it some other reason?Regards,Arvind.

  • Anonymoussays:

    July 27, 2011 at 6:03 pm

    These errors are coming because mail.jar is not included in class path is part of mail.jar. Also, you have included/copied GmailSMTPAppender twice, so that might be reason for Duplicate class error.Hope this helps- Abhinav

  • Anonymoussays:

    October 11, 2011 at 7:01 am

    Hello when i used GmailSMTPAppender i am getting following error. log4j:ERROR Error occured while sending e-mail notification.javax.mail.MessagingException: Exception reading response; nested exception is: PKIX path building failed: unable to find valid certification path to requested target at com.sun.mail.smtp.SMTPTransport.readServerResponse( at com.sun.mail.smtp.SMTPTransport.openServer( at com.sun.mail.smtp.SMTPTransport.protocolConnect( at javax.mail.Service.connect( at javax.mail.Service.connect( at com.dwp.cq.log4j.GmailSMTPAppender.send( at com.dwp.cq.log4j.GmailSMTPAppender.sendBuffer( at help me on this

  • Anonymoussays:

    October 13, 2011 at 1:17 pm

    Hi Abhinav GuptaIs there some sort of port for this appender for log4php that you know of. I really want to use gmail with it and it doesn't have an SMTPAppender like log4j have. I hope you can give me ideas where to start.

Leave a Reply

%d bloggers like this: