3

我在尝试让我的 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,它会恢复正常并且可以正常连接。谁能告诉我我做错了什么?谢谢。

4

1 回答 1

0

我猜您的 SMTP 守护程序正在侦听“真实”IP,例如 192.168.1.123,因此未绑定到localhost(127.0.0.1),如中所示

log4j.appender.smtp.SMTPHost=localhost

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

干杯,

于 2013-03-04T19:35:08.133 回答