SmtpAppender log4net using gmail smtp

2019-04-29 23:30发布

问题:

I configured log4net with a smtpAppender (gmail). The weird issue is that the smtpAppender is not working when I deploy the application on IIS 7.5.

I tried to test the connection with the gmail smtp using telnet command. So I can check if anything is blocked but the test worked fine.

My log config:

  <log4net>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <file value="C:\logs\MyLog.log"/>
      <appendToFile value="true"/>
      <maximumFileSize value="500KB"/>
      <maxSizeRollBackups value="2"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %logger - %message%newline"/>
      </layout>
    </appender>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">
      <to value="test1@test.com" />
      <from value="test2@test.com" />
      <subject value="Error logging message" />
      <smtpHost value="smtp.gmail.com" />
      <port value="587"/>
      <authentication value="Basic" />
      <username value="test05@gmail.com"/>
      <password value="password"/>
      <EnableSsl value="true" />
      <bufferSize value="1" />
      <lossy value="true" />
      <evaluator type="log4net.Core.LevelEvaluator,log4net">
        <threshold value="ERROR" />
      </evaluator>
      <layout type="log4net.Layout.PatternLayout,log4net">
        <conversionPattern value="%property{log4net:HostName} :: %level :: %message %newlineLogger: %logger%newlineThread: %thread%newlineDate: %date%newlineNDC: %property{NDC}%newline%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="RollingFile"/>
      <appender-ref ref="SmtpAppender"/>
    </root>
  </log4net>

After debugging I found the below exception:

log4net:ERROR [SmtpAppender] ErrorCode: GenericFailure. Error occurred while sending e-mail notification.
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
   at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
   at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
   at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at log4net.Appender.SmtpAppender.SendEmail(String messageBody)
   at log4net.Appender.SmtpAppender.SendBuffer(LoggingEvent[] events)

回答1:

Although this is not a direct answer to your question, I believe it could help you to detect the problem. Log4net fails in silence and this is by design. In order to get information about what's wrong you could enable log4net debugging

Check How do I enable log4net internal debugging? on log4net FAQS



回答2:

The problem was on the gmail server side. Gmail server was blocking my email because it was suspecting the address where I am using my email account.



回答3:

you need to configure the permission to allow sending email from your app in google developer portal



回答4:

I used the config and got error:

... Authentication Required. ...

and I find that in access setting in Google account, you need to enable Allow less secure apps to allow program to access your GMail.

It's same in Yahoo Mail, see How to set Allow apps that use less secure sign in.