Post Reply 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Send Email Using Log4j with example
02-06-2011, 08:56 AM
Post: #1
Send Email Using Log4j with example
This article explains how to write a very basic code to email the log statements in the shipped code using Log4j . This does not explain indepth knowledge on writing Log4j Configuration. But, this writeup will be more useful for the beginners who haven't use Log4j Configuration for EMAIL. 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.

Before start using Log4j,lets look into how to install the Log4j , Java Mail API and JavaBeans Activation Framework in your machine.


Download the Apache LOG4j from the link : Download Apache LOG4j
Download the Java Mail API from the link : Download mailapi.jar and smtp.jar
Download the JavaBeans Activation Framework from the link : Download activation.jar
Extract the archived files to some suitable directory into your machine.
Add the file ../log4j-1.2.15.jar to your CLASSPATH environment variable
Add the file mailapi.jar,smtp.jar and activation.jar to your CLASSPATH environment variable
Set up is done. Let's look into our first simple example program using LOG4j.
First,Create a file named with the following content.As per the code it should be in the following path

C:\\TestLogger\\etc\\, MAIL
#defines how othen emails are send
log4j.appender.MAIL.Subject=Test Email.
log4j.appender.MAIL.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

In the above propertie file input your SMPThost name.If needed change your FROM,TO and Subject in the file.

Create a Java class named EmailLogger .java in com.test package with the following content:

package com.test;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class EmailLogger {
private String _log4jProp = null;

private static org.apache.log4j.Logger log = Logger

public EmailLogger(String pPropertiesFile) {
try {
} catch (Exception e) {

public static void main(String[] args) {
EmailLogger obj = new EmailLogger(
log.error("Hello Test Mail");
Find all posts by this user
Quote this message in a reply
Post Reply 

Forum Jump:

Send your comments, Suggestions or Queries regarding this site at

Copyright © 2010 All Rights Reserved