4

I need to send a email when any Exception occurs in log file .This is my log4j.properties file

log4j.rootLogger=Info, EMAIL
# EMAIL
log4j.appender.EMAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.EMAIL.SMTPHost=smtp.gmail.com
log4j.appender.EMAIL.SMTPProtocol=smtp 
log4j.appender.EMAIL.SMTPPort=25 
log4j.appender.EMAIL.StartTls=true 
log4j.appender.EMAIL.From=krn1231@gmail.com
log4j.appender.EMAIL.To=krn1231@gmail.com
log4j.appender.EMAIL.SMTPUsername=krn1231
log4j.appender.EMAIL.SMTPPassword=mypassword
log4j.appender.EMAIL.Subject=Test Gmail Smtp
log4j.appender.EMAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.EMAIL.layout.ConversionPattern=[%d] [%t] %-5p %c %x - %m%n
log4j.appender.EMAIL.Threshold=ERROR
log4j.appender.EMAIL.BufferSize=1
log4j.appender.EMAIL.SMTPDebug=true

This is my sample Program for Testing this

import org.apache.log4j.Logger;

public class Test {

    private static Logger loggers = Logger.getLogger(Test.class);

    public static void main(String args[]) {

        try {
            loggers.info("Hisas");
            throw new NullPointerException();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

I tried all ways of trail and Error , but of no use

This is the output given inside Eclipse Console , when i tried

 log4j:WARN No such property [startTls] in org.apache.log4j.net.SMTPAppender.
DEBUG: setDebug: JavaMail version 1.4ea
java.lang.NullPointerException
    at Test.main(Test.java:11)

When i executed this on command prompt

telnet smtp.gmail.com 25

It responded well with

220 mx.google.com ESMTP f8sm802040pbe.42

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • Are you sure you are looking at the right log4j.properties file? Because your error message says, it cannot use "25 # NEW" as a value for the port but your posted log4j.properties only contains "25" as port value. – Joachim Rohde Mar 27 '12 at 18:00
  • 2
    possible duplicate of [Log4j failing to send an email when logging an error](http://stackoverflow.com/questions/6242838/log4j-failing-to-send-an-email-when-logging-an-error) – CoolBeans Mar 27 '12 at 18:02
  • ya , i have modified the question , sorry for the confusion created . – Pawan Mar 27 '12 at 18:02
  • @yyyi777 - Your questions are already answered in the question that I linked as [duplicate](http://stackoverflow.com/questions/6242838/log4j-failing-to-send-an-email-when-logging-an-error). Please refer to that one. You can close this question if the other one already satisfies your query. – CoolBeans Mar 27 '12 at 18:44

2 Answers2

4

pass the vm arguement first i.e -Dmail.smtp.starttls.enable=true

log4j.rootLogger=DEBUG, sendMail
      log4j.appender.sendMail=org.apache.log4j.net.SMTPAppender  
      log4j.appender.sendMail.SMTPProtocol=smtps
      log4j.appender.sendMail.Threshold=ERROR  
      log4j.appender.sendMail.SMTPPort=465
      log4j.appender.sendMail.SMTPUsername=xxxx@gmail.com 
      log4j.appender.sendMail.From=xxxx@gmail.com
      log4j.appender.sendMail.SMTPPassword=.........
      log4j.appender.sendMail.To=yyyyy@gmail.com 
      log4j.appender.sendMail.SMTPHost=smtp.gmail.com
      log4j.appender.sendMail.Subject=Error Alert  
      log4j.appender.sendMail.layout=org.apache.log4j.PatternLayout  
      log4j.appender.sendMail.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss.SSS} [%p] %t %c - %m%n
      log4j.appender.sendMail.smtp.starttls.enable=true
      log4j.appender.sendMail.smtp.auth=true
      log4j.appender.sendMail.BufferSize=1
madhu
  • 1,083
  • 3
  • 12
  • 31
3

Try the smtps protocol as Gmail requires SSL.

Stephane
  • 11,836
  • 25
  • 112
  • 175