Wednesday, July 02, 2008

Send Email Alerts Using Log4j [Howto]

It's really easy to add error alerting to your java application. If your application logs all errors using log4j and you want these errors emailed out to a support team or to yourself, then all you have to do is add another appender to your file.

Let's say you have the following class which logs an error message: And your log4j properties file is: Now add a new MAIL appender, so that your properties file looks like this: Don't forget to place mail.jar and activation.jar on your classpath and then run the application.

Check your inbox and voila, you have an alert without making any source code changes!

NB: By default, an email message will be sent when an ERROR or higher severity message is appended. If you want to email messages with levels less than ERROR (e.g. INFO) then you currently have to configure your own implementation of the TriggeringEventEvaluator. Setting log4j.appender.MAIL.Threshold=INFO will not work.

Further Reading:
SMTPAppender javadoc
SmtpAppender Members and Properties


  1. hi!
    I used the code you have posted to alert when an error occurs, also added activation and mail.jar to my classpath. I am using jdk 1.6_03JDK, and so when I use eclipse to run, it works like a charm. However when I generate a JAR using fat jar (a plugin for eclipse for generating jars), I get wierd errors.. posted below..
    any help here..
    Exception in thread "Thread-1" java.lang.NoSuchMethodError: com.sun.mail.util.So
    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 javax.mail.Service.connect(
    at javax.mail.Transport.send0(
    at javax.mail.Transport.send(
    at Source)
    at Source)
    at org.apache.log4j.AppenderSkeleton.doAppend(

    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders
    at org.apache.log4j.Category.callAppenders(
    at org.apache.log4j.Category.forcedLog(
    at org.apache.log4j.Category.error(
    at Source)

  2. Anonymous12:56 AM

    Is there a way to disable sending email message when an error occurs.

  3. Setting log4j.appender.MAIL.Threshold=ERROR will prevent sending emails for debug statements when you have logging cranked up to DEBUG level.

  4. This type of lace wig looks very realistic and is made from sophisticated lace material having the original look. Not just full lace wigs,lace front wigs,full lace wigs gives a natural character but lace wigs and lace front wigs also render a fashionable look to the face. The lace wigs in this wig usually appears as if it is growing out of the scalp and equally distributed. Some manufacturers now are trying to make their lace front human hair wigs inexpensive so that ladies who really need it can afford to buy it.

  5. Anonymous4:37 PM

    In a multi-server environment situation, is there a way that log4j logs the name of the server?

  6. I want to add a throttling mechanism for sending emails that will be added by extending the “TriggerEvaluator” class and adding a timing mechanism for sending emails related to the same trigger / error.

    How and where to do this?

  7. Hi Fahd,

    I followed the code that u posted. But I'm not able to achieve it.. I am not getting any email when it throws log.error().