Log4j SMTP将错误消息发送到电子邮件

时间:2013-03-04 18:52:29

标签: java smtp log4j

我遇到一个主要问题,试图让我的Log4j在出现问题时发送SMTP电子邮件。这是我的代码:

final Properties properties = new Properties();
final String strProperties = "log4j.rootLogger=ERROR, console, smtp\n"
            + "log4j.appender.console=org.apache.log4j.ConsoleAppender\n"
            + "log4j.appender.console.Target=System.out\n"
            + "log4j.appender.console.layout=org.apache.log4j.PatternLayout\n"
            + "log4j.appender.console.layout.ConversionPattern=%-5p: %c - %m%n\n"
            + "log4j.appender.smtp=org.apache.log4j.net.SMTPAppender\n"
            + "log4j.appender.smtp.EvaluatorClass=com.blah.blah.log4j.TriggerOnError\n"
            + "log4j.appender.smtp.SMTPHost=localhost\n"
            + "log4j.appender.smtp.From=mml_automation@blah.com\n"
            + "log4j.appender.smtp.To=ccmis@blah.com\n"
            + "log4j.appender.smtp.Subject=BagBox ERROR\n"
            + "log4j.appender.smtp.layout=org.apache.log4j.PatternLayout\n"
            + "log4j.appender.smtp.threshold=INFO\n"
            + "log4j.appender.smtp.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} : %-40c : %-5p %x - %m %n\n"
            + "log4j.logger.com.blah.blah.service.BagBoxService=INFO, smtp\n"
            + "log4j.logger.com.blah.blah.security=INFO, smtp\n"
            + "log4j.logger.org.springframework.core=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.beans=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.context=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.web=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.web.servlet.DispatcherServlet=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.security=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.aop=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.ui=ERROR, console, smtp\n";

    final ByteArrayInputStream byteProperties = new ByteArrayInputStream(
            strProperties.getBytes());
    properties.load(byteProperties);
    PropertyConfigurator.configure(properties);

我在控制台中一直收到以下错误:

Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
java.net.SocketException: Network is unreachable: connect

如果我在IP地址上运行telnet,它会恢复正常并且连接正常。谁能告诉我我做错了什么?感谢。

1 个答案:

答案 0 :(得分:0)

我猜你的SMTP守护程序正在侦听“真实”的IP,例如192.168.1.123,因此没有绑定到localhost(127.0.0.1),如

所示
log4j.appender.smtp.SMTPHost=localhost

netstat -anpt | grep 25告诉你什么?您是否可以在25以外的端口上运行SMTP?

干杯,