title: Log4j2 SMTP-Appender "Must issue a STARTTLS command first"
body: <p>I am trying to send log entries with severity "error" via email in java. 
  In my log4j2.xml file I have the following SMTP appender:</p>
  
  <pre><code>&lt;SMTP&gt;
  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"&gt;
  &lt;/SMTP&gt;
  </code></pre>
  
  <p>But when I run my programm (which consists of a main logging a string) the following error occurs:</p>
  
  <pre><code>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:&lt;sender@domain.com&gt;
  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
  </code></pre>
  
  <p>As far as I understand the programm can establish a connection to the server but then <code>Must issue a STARTTLS command first</code> this happens. Can someone explain to me what that means and how to fix it?</p>
  
  <p>When I searched for the error I got a few posts with similar problems but not with log4j2 but with java.mail and they didn't helped me.</p>
  
  <p>I tried using <code>smtpPort="465"</code> and <code>smtpProtocol="smtps"</code> but in both cases the connection got refused.</p>
  
  <p>I have no more new ideas and am grateful for everyone who helps me.</p>