Log4net html formatted SmtpAppender

后端 未结 2 1593
猫巷女王i
猫巷女王i 2021-01-12 12:58

I am trying to send me email in HTML format.

    

  

        
2条回答
  •  死守一世寂寞
    2021-01-12 13:29

    Since log4net isn't supporting HTML format in the message body so far, I wrote my own appender:

    using System;
    using System.Net.Mail;
    using log4net.Appender;
    
    namespace log4net.Appender
    {
        public class ExtendedSmtpAppender : SmtpAppender
        {
            public bool IsBodyHtml { get; set; }
    
            protected override void SendEmail(string messageBody)
            {
                // .NET 2.0 has a new API for SMTP email System.Net.Mail
                // This API supports credentials and multiple hosts correctly.
                // The old API is deprecated.
    
                // Create and configure the smtp client
                SmtpClient smtpClient = new SmtpClient();
                if (!String.IsNullOrEmpty(SmtpHost))
                {
                    smtpClient.Host = SmtpHost;
                }
                smtpClient.Port = Port;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.EnableSsl = EnableSsl;
    
                if (Authentication == SmtpAuthentication.Basic)
                {
                    // Perform basic authentication
                    smtpClient.Credentials = new System.Net.NetworkCredential(Username, Password);
                }
                else if (Authentication == SmtpAuthentication.Ntlm)
                {
                    // Perform integrated authentication (NTLM)
                    smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                }
    
                using (MailMessage mailMessage = new MailMessage())
                {
                    mailMessage.IsBodyHtml = IsBodyHtml;
                    mailMessage.Body = messageBody;
                    //mailMessage.BodyEncoding = BodyEncoding;
                    mailMessage.From = new MailAddress(From);
                    mailMessage.To.Add(To);
                    if (!String.IsNullOrEmpty(Cc))
                    {
                        mailMessage.CC.Add(Cc);
                    }
                    if (!String.IsNullOrEmpty(Bcc))
                    {
                        mailMessage.Bcc.Add(Bcc);
                    }
                    if (!String.IsNullOrEmpty(ReplyTo))
                    {
                        // .NET 4.0 warning CS0618: 'System.Net.Mail.MailMessage.ReplyTo' is obsolete:
                        // 'ReplyTo is obsoleted for this type.  Please use ReplyToList instead which can accept multiple addresses. http://go.microsoft.com/fwlink/?linkid=14202'
    #if !FRAMEWORK_4_0_OR_ABOVE
                        mailMessage.ReplyTo = new MailAddress(ReplyTo);
    #else
                        mailMessage.ReplyToList.Add(new MailAddress(m_replyTo));
    #endif
                    }
                    mailMessage.Subject = Subject;
                    //mailMessage.SubjectEncoding = m_subjectEncoding;
                    mailMessage.Priority = Priority;
    
                    // TODO: Consider using SendAsync to send the message without blocking. This would be a change in
                    // behaviour compared to .NET 1.x. We would need a SendCompletedCallback to log errors.
                    smtpClient.Send(mailMessage);
                }
            }
        }
    }
    

    To use this appender in your log4net config, just replace namespace and class name of your standard SmtpAppender definition and add the isBodyHtml option as follows:

    
        ...
        
        ...
    
    

    The SendEmail method body source is taken from the latest version of the standard log4net's SmtpAppender. I only removed old Framework versions supporting stuff and added this string:

    mailMessage.IsBodyHtml = IsBodyHtml;
    

    You can get the latest version of the standard log4net's SmtpAppender here.

    UPDATE:
    Below is a sample config (inspired with these examples):

    
      
      
        
        
        
        
        
        
        
        
          
        
        
          
        
      
    
      
      
        
        
      
    
    

提交回复
热议问题