Log4j2 SMTP-Appender "Must issue a STARTTLS command first"

Log4j2 SMTP-Appender "Must issue a STARTTLS command first"

我正在尝试通过 java 中的电子邮件发送严重性为 "error" 的日志条目。 在我的 log4j2.xml 文件中,我有以下 SMTP 附加程序:

<SMTP>
name="Mail"
subject="Error Log"
to="receiver@domain.com"
from="sender@domain.com"
smtpHost="SMTP-Host"
smtpPort="587"
smtpPassword="[password]" 
smtpUsername="email@domain.com"
smtpDebug="true"
ignoreExceptions="false"
bufferSize="4"
smtpProtocol="smtp">
</SMTP>

但是当我运行我的程序(由一个主要记录字符串组成)时出现以下错误:

DEBUG: setDebug: JavaMail version 1.4.7
13:13:44.469 [main] ERROR Main - Test Error
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "SMTP-Host", port 587, isSSL false
220 SMTP-Host ESMTP Postfix (Debian/GNU)
DEBUG SMTP: connected to host "SMTP-Host", port: 587

EHLO MAXPC
250-SMTP-Host
250-PIPELINING
250-SIZE 52428800
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<sender@domain.com>
530 5.7.0 Must issue a STARTTLS command first
DEBUG SMTP: got response code 530, with response: 530 5.7.0 Must issue a STARTTLS command first

RSET
530 5.7.0 Must issue a STARTTLS command first
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.logging.log4j.core.net.SmtpManager.sendMultipartMessage(SmtpManager.java:257)
    at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:172)
    at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:181)
    at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
    at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464)
    at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406)
    at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
    at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
    at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2007)
    at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1866)
    at org.apache.logging.slf4j.Log4jLogger.error(Log4jLogger.java:299)
    at Main.main(Main.java:8)
QUIT
221 2.0.0 Bye
2019-03-03 13:13:49,204 main ERROR SmtpManager SMTP:848b9401aab8a35f62b00f0dbdf21fd4 Caught exception while sending e-mail notification.: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
 com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.logging.log4j.core.net.SmtpManager.sendMultipartMessage(SmtpManager.java:257)
    at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:172)
    at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:181)
    at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
    at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464)
    at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406)
    at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
    at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
    at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2007)
    at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1866)
    at org.apache.logging.slf4j.Log4jLogger.error(Log4jLogger.java:299)
    at Main.main(Main.java:8)

2019-03-03 13:13:49,206 main ERROR An exception occurred processing Appender Mail org.apache.logging.log4j.LoggingException: Error occurred while sending email
    at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:175)
    at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:181)
    at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
    at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464)
    at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406)
    at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
    at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
    at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2007)
    at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1866)
    at org.apache.logging.slf4j.Log4jLogger.error(Log4jLogger.java:299)
    at Main.main(Main.java:8)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.logging.log4j.core.net.SmtpManager.sendMultipartMessage(SmtpManager.java:257)
    at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:172)
    ... 18 more

Exception in thread "main" org.apache.logging.log4j.core.appender.AppenderLoggingException: An exception occurred processing Appender Mail
    at org.apache.logging.log4j.core.appender.DefaultErrorHandler.error(DefaultErrorHandler.java:75)
    at org.apache.logging.log4j.core.config.AppenderControl.handleAppenderError(AppenderControl.java:165)
    at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:158)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
    at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464)
    at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406)
    at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
    at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
    at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2007)
    at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1866)
    at org.apache.logging.slf4j.Log4jLogger.error(Log4jLogger.java:299)
    at Main.main(Main.java:8)
Caused by: org.apache.logging.log4j.LoggingException: Error occurred while sending email
    at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:175)
    at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:181)
    at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
    ... 16 more
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.logging.log4j.core.net.SmtpManager.sendMultipartMessage(SmtpManager.java:257)
    at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:172)
    ... 18 more

据我了解,程序可以与服务器建立连接,但随后 Must issue a STARTTLS command first 发生了这种情况。有人可以向我解释这意味着什么以及如何解决它吗?

当我搜索错误时,我得到了一些类似问题的帖子,但不是 log4j2,而是 java.mail,他们没有帮助我。

我尝试使用 smtpPort="465"smtpProtocol="smtps" 但在这两种情况下连接都被拒绝了。

我没有更多的新想法,感谢所有帮助我的人。

我遇到了同样的问题,看起来 log4j2 无法处理 STARTTLS。幸运的是,log4j 在创建邮件会话时使用了系统属性。所以我的修复是使用

smtpProtocol="smtp"

用于 SMTP appender 和设置系统 属性

mail.smtp.starttls.enable=真

在命令行上为邮件会话激活 STARTTLS。您也可以使用

System.setProperty("mail.smtp.starttls.enable", "true")

在您的应用程序代码中。