A log4net appender that uses SmtpClient

Google Apps provides free email hosting for small businesses.  However, their SMTP server requires SSL authentication for sending outbound emails, so you can't use use log4net to send emails based on the content of logged messages.  Ron Grabowski suggested writing a log4net appender that uses SmtpClient (only available in 2.0) to send SSL secured messages. The below class works for me against smtp.google.com using port 587:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using log4net.Appender;

using log4net.Core;

using System.Net.Mail;

using System.Net;

namespace log4netExtensions

{

  /// <summary>

  /// The standard log4net SmtpAppender doesn't support SSL authentication, which is

  /// required to send email via gmail.

  ///

  /// This appender uses the SmtpClient (only available in .NET 2.0) to send SMTP mail that

  /// is secured via SSL.  This is needed to talk to the gmail SMTP server. 

  ///

  /// This code is heavily based on that posted by Ron Grabowski at:

  /// http://mail-archives.apache.org/mod_mbox/logging-log4net-user/200602.mbox/%3C20060216123155.22007.qmail@web32202.mail.mud.yahoo.com%3E

  /// </summary>

  public class SmtpClientSmtpAppender : SmtpAppender

  {

    override protected void SendBuffer(LoggingEvent[] events)

    {

      try

      {

        StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);

        string t = Layout.Header;

        if (t != null)

        {

          writer.Write(t);

        }

        for (int i = 0; i < events.Length; i++)

        {

          // Render the event and append the text to the buffer

          RenderLoggingEvent(writer, events[i]);

        }

        t = Layout.Footer;

        if (t != null)

        {

          writer.Write(t);

        }

        // Use SmtpClient so we can use SSL.

        SmtpClient client = new SmtpClient(SmtpHost, Port);

        client.EnableSsl = true;

        client.Credentials = new NetworkCredential(Username, Password);

        string messageText = writer.ToString();

        MailMessage mail = new MailMessage(From, To, Subject, messageText);

        client.Send(mail);

      }

      catch (Exception e)

      {

        ErrorHandler.Error("Error occurred while sending e-mail notification from SmtpClientSmtpAppender.", e);

      }

    }

  }

}

4 Comments

Comments have been disabled for this content.