0
votes

I try to use the SmtpAppender in LogNet:

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="[email protected],[email protected]" />
      <from value="[email protected]" />
      <subject value="Error" />
      <smtpHost value="smtp.company.com" />
      <bufferSize value="512" />
      <lossy value="true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="DEBUG"/>
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
      </layout>
    </appender>

When I enable debug logging in Log4net I receive the following error:

log4net:ERROR [SmtpAppender] ErrorCode: GenericFailure. Error occurred while sending e-mail notification. System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions (IP from SMTP-Server):25 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address,Socket& abortSocket, Socket& abortSocket6) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at log4net.Appender.SmtpAppender.SendEmail(String messageBody) at log4net.Appender.SmtpAppender.SendBuffer(LoggingEvent[] events)

However. When I try to send a mail through the same SMTP-Server from within my program that uses this log4net-configuration:

MailMessage msg = new MailMessage(sender, recipients, subject, body);
mtpClient client = new SmtpClient("smtp.company.com", 25);
client.Send(msg);

everything works fine. So this could not be a simple SMTP-Server/FireWall-Issue.

Is the SMTPAppender working differently that the standard StmpClient.Send() from .NET that could cause this issue?

1
If you look at the stack trace, it looks like the SMTPAppender is also just calling SmtpClient.Send.Bob G

1 Answers

0
votes

@Bob Geiger pointed me into the right direction that Log4net seems to use exactly the same mechanism.

As it turns out this was a Virusscan/Spam-Protection issue. Only defined applications where allowed to send mails through our smtp-server. While one program of us worked which used the same backend as the log4net - application it hat a different filename and so was allowed to send mails, while the other program wasn't.

We had to allow the specific .exe - file in the virusscan-configuration.