4

当日志文件中发生任何异常时,我需要发送电子邮件。这是我的 log4j.properties 文件

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

这是我的测试示例程序

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();
        }

    }

}

我尝试了所有的 trail 和 Error 方法,但没有用

这是我尝试时在 Eclipse 控制台中给出的输出

 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)

当我在命令提示符下执行这个

远程登录 smtp.gmail.com 25

它的反应很好

220 mx.google.com ESMTP f8sm802040pbe.42

4

2 回答 2

4

首先通过 vm 论证,即-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
于 2015-01-02T13:34:01.207 回答
3

尝试使用 smtps 协议,因为 Gmail 需要 SSL。

于 2012-04-21T17:32:19.827 回答