当出现问题时,我在尝试让 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,它会正常返回并且可以正常连接。谁能告诉我我做错了什么?谢谢。
最佳答案
我猜测您的 SMTP 守护程序正在监听“真实”IP,例如 192.168.1.123,因此未绑定(bind)到localhost
(127.0.0.1),如
log4j.appender.smtp.SMTPHost=localhost
netstat -anpt | 是什么意思? grep 25 告诉你吗?您是否可能在 25 以外的端口上运行 SMTP?
干杯,
关于java - Log4j SMTP 将错误消息发送到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15208883/