Created
April 5, 2013 16:11
-
-
Save ferventcoder/5320517 to your computer and use it in GitHub Desktop.
If you are not ready to upgrade to 1.2.11, you can always explore this option.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// This is a custom appender so that we can set 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<appender name="ErrorSmtpAppender" type="SomeProject.Infrastructure.Logging.Log4netAppenders.SmtpCustomAppender"> | |
<authentication value="Basic" /> | |
<smtpHost value="mail.gmail.com" /> | |
<username value="[email protected]" /> | |
<password value="somepassword" /> | |
<enablessl value="true" /> | |
<port value="587" /> | |
<to value="some folks" /> | |
<from value="[email protected]" /> | |
<subject value="Some subject - Environment" /> | |
<bufferSize value="2" /> | |
<lossy value="true" /> | |
<evaluator type="log4net.Core.LevelEvaluator"> | |
<threshold value="ERROR" /> | |
</evaluator> | |
<layout type="log4net.Layout.PatternLayout"> | |
<conversionPattern value="%newline%date [%thread] %-5level %logger - %message%newline" /> | |
<!--<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />--> | |
</layout> | |
</appender> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment