我想将日志文件发送到给定的电子邮件地址。这是我尝试的方式。但它没有用。
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.net.SMTPAppender;
public class LogSender {
static Logger logger = Logger.getLogger(test.class);
SMTPAppender appender = new SMTPAppender();
public test() {
try {
appender.setTo("receiver@gmail.com");
appender.setFrom("sender@gmail.com");
appender.setSMTPHost("smtp.gmail.com");
appender.setSMTPUsername("sender@gmail.com");
appender.setSMTPPassword("mypassword");
appender.setLocationInfo(true);
appender.setSubject("Test Mail From Log4J");
appender.setLayout(new PatternLayout());
appender.activateOptions();
logger.addAppender(appender);
logger.error("This is an error");
}
catch(Exception e) {
e.printStackTrace();
logger.error("Thrown exception",e);
}
}
public static void main(String args[]) {
LogSender l = new LogSender;
}
}
有一个例外说
log4j:ERROR Error occured while sending e-mail notification.
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. ee6sm12133321igc.6
任何人都告诉我问题所在,以及我如何解决这个问题或者请给我任何可用的示例代码。