123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665 |
- #region Apache License
- #endregion
- #if !NETCF && !SSCLI
- using System;
- using System.IO;
- using System.Text;
- #if NET_2_0 || MONO_2_0
- using System.Net.Mail;
- #else
- using System.Web.Mail;
- #endif
- using log4net.Layout;
- using log4net.Core;
- using log4net.Util;
- namespace log4net.Appender
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class SmtpAppender : BufferingAppenderSkeleton
- {
- #region Public Instance Constructors
-
-
-
-
-
-
-
-
- public SmtpAppender()
- {
- }
- #endregion // Public Instance Constructors
- #region Public Instance Properties
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public string To
- {
- get { return m_to; }
- set { m_to = MaybeTrimSeparators(value); }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public string Cc
- {
- get { return m_cc; }
- set { m_cc = MaybeTrimSeparators(value); }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public string Bcc
- {
- get { return m_bcc; }
- set { m_bcc = MaybeTrimSeparators(value); }
- }
-
-
-
-
-
-
-
-
-
-
-
- public string From
- {
- get { return m_from; }
- set { m_from = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
- public string Subject
- {
- get { return m_subject; }
- set { m_subject = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public string SmtpHost
- {
- get { return m_smtpHost; }
- set { m_smtpHost = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
- [Obsolete("Use the BufferingAppenderSkeleton Fix methods")]
- public bool LocationInfo
- {
- get { return false; }
- set { ; }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public SmtpAuthentication Authentication
- {
- get { return m_authentication; }
- set { m_authentication = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
- public string Username
- {
- get { return m_username; }
- set { m_username = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
- public string Password
- {
- get { return m_password; }
- set { m_password = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
- public int Port
- {
- get { return m_port; }
- set { m_port = value; }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public MailPriority Priority
- {
- get { return m_mailPriority; }
- set { m_mailPriority = value; }
- }
- #if NET_2_0 || MONO_2_0
-
-
-
-
-
-
- public bool EnableSsl
- {
- get { return m_enableSsl; }
- set { m_enableSsl = value; }
- }
-
-
-
-
-
-
- public string ReplyTo
- {
- get { return m_replyTo; }
- set { m_replyTo = value; }
- }
- #endif
-
-
-
-
-
-
- public Encoding SubjectEncoding
- {
- get { return m_subjectEncoding; }
- set { m_subjectEncoding = value; }
- }
-
-
-
-
-
-
- public Encoding BodyEncoding
- {
- get { return m_bodyEncoding; }
- set { m_bodyEncoding = value; }
- }
- #endregion // Public Instance Properties
- #region Override implementation of BufferingAppenderSkeleton
-
-
-
-
- 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++)
- {
-
- RenderLoggingEvent(writer, events[i]);
- }
- t = Layout.Footer;
- if (t != null)
- {
- writer.Write(t);
- }
- SendEmail(writer.ToString());
- }
- catch(Exception e)
- {
- ErrorHandler.Error("Error occurred while sending e-mail notification.", e);
- }
- }
- #endregion // Override implementation of BufferingAppenderSkeleton
- #region Override implementation of AppenderSkeleton
-
-
-
-
-
-
-
-
-
- override protected bool RequiresLayout
- {
- get { return true; }
- }
- #endregion // Override implementation of AppenderSkeleton
- #region Protected Methods
-
-
-
-
- virtual protected void SendEmail(string messageBody)
- {
- #if NET_2_0 || MONO_2_0
-
-
-
-
- SmtpClient smtpClient = new SmtpClient();
- if (!String.IsNullOrEmpty(m_smtpHost))
- {
- smtpClient.Host = m_smtpHost;
- }
- smtpClient.Port = m_port;
- smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
- smtpClient.EnableSsl = m_enableSsl;
- if (m_authentication == SmtpAuthentication.Basic)
- {
-
- smtpClient.Credentials = new System.Net.NetworkCredential(m_username, m_password);
- }
- else if (m_authentication == SmtpAuthentication.Ntlm)
- {
-
- smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
- }
- using (MailMessage mailMessage = new MailMessage())
- {
- mailMessage.Body = messageBody;
- mailMessage.BodyEncoding = m_bodyEncoding;
- mailMessage.From = new MailAddress(m_from);
- mailMessage.To.Add(m_to);
- if (!String.IsNullOrEmpty(m_cc))
- {
- mailMessage.CC.Add(m_cc);
- }
- if (!String.IsNullOrEmpty(m_bcc))
- {
- mailMessage.Bcc.Add(m_bcc);
- }
- if (!String.IsNullOrEmpty(m_replyTo))
- {
-
-
- #if !NET_4_0 && !MONO_4_0
- mailMessage.ReplyTo = new MailAddress(m_replyTo);
- #else
- mailMessage.ReplyToList.Add(new MailAddress(m_replyTo));
- #endif
- }
- mailMessage.Subject = m_subject;
- mailMessage.SubjectEncoding = m_subjectEncoding;
- mailMessage.Priority = m_mailPriority;
-
-
- smtpClient.Send(mailMessage);
- }
- #else
-
- MailMessage mailMessage = new MailMessage();
- mailMessage.Body = messageBody;
- mailMessage.BodyEncoding = m_bodyEncoding;
- mailMessage.From = m_from;
- mailMessage.To = m_to;
- if (m_cc != null && m_cc.Length > 0)
- {
- mailMessage.Cc = m_cc;
- }
- if (m_bcc != null && m_bcc.Length > 0)
- {
- mailMessage.Bcc = m_bcc;
- }
- mailMessage.Subject = m_subject;
- #if !MONO && !NET_1_0 && !NET_1_1 && !CLI_1_0
- mailMessage.SubjectEncoding = m_subjectEncoding;
- #endif
- mailMessage.Priority = m_mailPriority;
- #if NET_1_1
-
-
-
-
-
-
- try
- {
- if (m_authentication == SmtpAuthentication.Basic)
- {
-
- mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
- mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", m_username);
- mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", m_password);
- }
- else if (m_authentication == SmtpAuthentication.Ntlm)
- {
-
- mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 2);
- }
-
- if (m_port != 25)
- {
- mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", m_port);
- }
- }
- catch(MissingMethodException missingMethodException)
- {
-
-
- ErrorHandler.Error("SmtpAppender: Authentication and server Port are only supported when running on the MS .NET 1.1 framework", missingMethodException);
- }
- #else
- if (m_authentication != SmtpAuthentication.None)
- {
- ErrorHandler.Error("SmtpAppender: Authentication is only supported on the MS .NET 1.1 or MS .NET 2.0 builds of log4net");
- }
- if (m_port != 25)
- {
- ErrorHandler.Error("SmtpAppender: Server Port is only supported on the MS .NET 1.1 or MS .NET 2.0 builds of log4net");
- }
- #endif // if NET_1_1
- if (m_smtpHost != null && m_smtpHost.Length > 0)
- {
- SmtpMail.SmtpServer = m_smtpHost;
- }
- SmtpMail.Send(mailMessage);
- #endif // if NET_2_0
- }
- #endregion // Protected Methods
- #region Private Instance Fields
- private string m_to;
- private string m_cc;
- private string m_bcc;
- private string m_from;
- private string m_subject;
- private string m_smtpHost;
- private Encoding m_subjectEncoding = Encoding.UTF8;
- private Encoding m_bodyEncoding = Encoding.UTF8;
-
- private SmtpAuthentication m_authentication = SmtpAuthentication.None;
- private string m_username;
- private string m_password;
-
- private int m_port = 25;
- private MailPriority m_mailPriority = MailPriority.Normal;
- #if NET_2_0 || MONO_2_0
- private bool m_enableSsl = false;
- private string m_replyTo;
- #endif
- #endregion // Private Instance Fields
- #region SmtpAuthentication Enum
-
-
-
-
-
-
-
-
- public enum SmtpAuthentication
- {
-
-
-
- None,
-
-
-
-
-
-
- Basic,
-
-
-
-
-
-
- Ntlm
- }
- #endregion // SmtpAuthentication Enum
- private static readonly char[] ADDRESS_DELIMITERS = new char[] { ',', ';' };
-
-
-
-
- private static string MaybeTrimSeparators(string s) {
- #if NET_2_0 || MONO_2_0
- return string.IsNullOrEmpty(s) ? s : s.Trim(ADDRESS_DELIMITERS);
- #else
- return s != null && s.Length > 0 ? s : s.Trim(ADDRESS_DELIMITERS);
- #endif
- }
- }
- }
- #endif // !NETCF && !SSCLI
|