Gmail requires SMTP connections to be secure. If you try to use log4net‘s SmtpAppender to connect to the Gmail smtp server and get an error like the following:
log4net:ERROR [SmtpAppender] Error occurred while sending e-mail notification. System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at log4net.Appender.SmtpAppender.SendEmail(String messageBody)
the most common cause is failure to add the authentication line to your configuration:
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender"> <to value="foo@bar.com" /> <from value="baz@bar.com" /> <subject value="Some subject" /> <smtpHost value="smtp.gmail.com" /> <authentication value="Basic" /> ...
If you try to connect to gmail without SSL you will receive a message like the following:
log4net:ERROR [SmtpAppender] Error occurred while sending e-mail notification. System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. o47sm1936887yhn.72 at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at log4net.Appender.SmtpAppender.SendEmail(String messageBody)
In this case the problem is the latest official release of log4net (version 1.2.10 at the time of this posting) does not provide a way to configure the SmtpAppender to send mail using SSL.
It turns out a fix for this problem was checked in to the log4Net subversion repository in June 2008 but no official build contains the fix.
One solution is to get the latest log4net source code from its subversion repository, compile for .Net 2.0, and you’re in business… but that might cause you other headaches if they unexpectedly changed something else that you were using.
The alternative is to download the source of the latest public release (1.2.10) and patch it yourself as follows:
The fix involves making two small changes to the log4net code, and adding a corresponding value to your log4net configuration.
1) download the source code from the Apache web site
2) unzip it
3) open the sln file (under src) with Visual Studio
4) find the SmtpAppender class (under log4net/Appender)
5) search in that class for “public MailPriority Priority”
6) add the following new property after that it:
public bool EnableSsl { get; set; }
7) search for “smtpClient.Send(mailMessage)”
Notice that this segment of code is grayed-out. That’s because the project has a conditional compilation symbol named NET_1_0 that is enabled by default and this code segment is enclosed in a conditional compilation symbol named NET_2_0. We need to enable this segment of code. To do that:
7a) right click on the log4net project in the solution explorer and select “Properties”
7b) change the conditional compilation symbol version
7c) close the properties tab
7d) the code segment should no longer appear grayed-out
7e) above the “smtpClient.Send” line you searched for add the following:
smtpClient.EnableSsl = EnableSsl;
8 ) when you try to compile you’ll get an error in XmlConfigurator related to System.Configuration.ConfigurationManager. This is because the project does not have a reference to the System.Configuration assembly. Add the reference.
9) you’ll also get an error related to a missing NUnit reference in the test project. You can add one and run the tests – several fail by the way but none are related to the change we made, they were checked in that way. Seriously. Anyway, you could also skip that part and compile only the log4net project.
10) replace the log4net assembly in your project with the one from build\bin\net\1.0\debug
11) update the SmtpAppender segment of your app.config to include the EnableSsl setting
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender"> <to value="foo@bar.com" /> <from value="baz@bar.com" /> <subject value="Some subject" /> <smtpHost value="smtp.gmail.com" /> <authentication value="Basic" /> <port value="587" /> <username value="gmail user name" /> <password value="gmail password" /> <bufferSize value="1" /> <EnableSsl value="true"/> <lossy value="true" /> <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="ERROR"/> </evaluator> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" /> </layout> </appender>
12) recompile your project if necessary
13) trigger whatever it is that you expect to cause something to be logged to log4net and it should work.
If it still doesn’t work, make sure you have a reference to SmtpAppender in one of your configured log4net categories, and/or in the root category.
<root> <level value="WARN"/> <appender-ref ref="RollingLogFileAppender"/> <appender-ref ref="SmtpAppender"/> </root>
And that the log level you are using in your code is at least as high as the highest one configured in the SmtpAppender and related category.
... <lossy value="true" /> <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="ERROR"/> </evaluator> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" /> </layout> </appender> <root> <level value="WARN"/> <appender-ref ref="RollingLogFileAppender"/> <appender-ref ref="SmtpAppender"/> </root>
Excellent article but this functionality is now available in log4net 1.2.11.0 so you no longer need to patch the component
Upgraded to 1.2.11 after reading your article and everything works. Thanks!