9

As stated in the title, I've had some issues finding an appender that states that it support the TLS encryption standard.

I've been trying to use the SmtpAppender but can't get it to work and suspect it has to do with the smtp server requiring TLS encryption and SmtpAppender might not supporting this.

setup from Office365 manual

The Manual

The configuration used:

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="my email" />
  <from value="The senders email" />
  <Username value="JohnDoe"></Username>
  <password value="NoWay" ></password>
  <authentication value="Basic"></authentication>
  <subject value="Test message" />
  <smtpHost value="pod51011.outlook.com" />
  <port value="587" />
  <bufferSize value="512" />
  <lossy value="true" />
  <EnableSsl value="true"/>
  <evaluator type="log4net.Core.LevelEvaluator">
      <threshold value="INFO"/>
  </evaluator>
  <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
  </layout>
</appender>
Philipp M
  • 1,877
  • 7
  • 27
  • 38
Rune Andersen
  • 421
  • 5
  • 10

2 Answers2

6

After some extra researching and actually testing using the newest Log4Net version 1.2.11 the answer is: Yes.

The question is somewhat answered here C# ASP.NET Send Email via TLS and looking into the issue tracking of Log4Net allow smtp to ssl authenticate and with certificates. it is documented that Log4Net has a switch EnableSsl which, as implied by the name, enables SSL supporty which by first link falls under TLS.

Community
  • 1
  • 1
Rune Andersen
  • 421
  • 5
  • 10
2

Another option for those not ready to upgrade to 1.2.11 is to add a custom appender:

/// <summary>
/// This is a custom appender so that we can enable SSL properly (and support TLS)
/// </summary>
public class SmtpCustomAppender : SmtpAppender
{
  public bool EnableSsl { get; set; }

  public SmtpCustomAppender()
  {
      Authentication = SmtpAuthentication.None;
      Port = 25; //0x19;
      //Port = 587; // 0x24b;
      Priority = MailPriority.Normal;
      EnableSsl = false;
  }

  /// <summary>
  /// Send the email message - this overrides the email sender so that we can add enabling SSL
  /// </summary>
  /// <param name="messageBody">the body text to include in the mail</param>
  protected override void SendEmail(string messageBody)
  {
      SmtpClient client = new SmtpClient();
      if (!string.IsNullOrEmpty(SmtpHost))
      {
          client.Host = SmtpHost;
      }
      client.Port = Port;
      client.EnableSsl = EnableSsl;
      client.DeliveryMethod = SmtpDeliveryMethod.Network;
      switch (Authentication)
      {
          case SmtpAuthentication.Basic:
              client.Credentials = new NetworkCredential(Username, Password);
              break;
          case SmtpAuthentication.Ntlm:
              client.Credentials = CredentialCache.DefaultNetworkCredentials;
              break;
      }

      MailMessage message = new MailMessage
          {
              Body = messageBody,
              From = new MailAddress(From)
          };
      message.To.Add(To);
      message.Subject = Subject;
      message.Priority = Priority;
      client.Send(message);
  }
}

Then in your config file, you just need to set what used to be the SmtpAppender to the fully qualified namespace of your custom appender.

<appender name="ErrorSmtpAppender" 
          type="SomeProject.Infrastructure.Logging.Log4netAppenders.SmtpCustomAppender">

Then you can add <enablessl value="true" /> to your log appender.

I created a gist to show you what this looks like.

Philipp M
  • 1,877
  • 7
  • 27
  • 38
ferventcoder
  • 11,952
  • 3
  • 57
  • 90