Log4j: Unable to send mail using Log4j SMTP Appenders

Pawan picture Pawan · Mar 27, 2012 · Viewed 8.9k times · Source

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 
[email protected]
[email protected]
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

Answer

madhu picture madhu · Jan 2, 2015

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
      [email protected] 
      [email protected]
      log4j.appender.sendMail.SMTPPassword=.........
      [email protected] 
      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