SmtpAppender无法发送通过SMTP服务器的电子邮件,而SmtpClient工作

SmtpAppender无法发送通过SMTP服务器的电子邮件,而SmtpClient工作

问题描述:

我尝试使用LogNet的SmtpAppender:SmtpAppender无法发送通过SMTP服务器的电子邮件,而SmtpClient工作

<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> 

当我在log4net的启用调试日志记录我收到以下错误:

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)

然而。当我尝试通过相同的SMTP-服务器从我的程序使用此log4net的配置中发送邮件:

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

一切工作正常。所以这不能是一个简单的SMTP服务器/防火墙问题。

SMTPAppender的工作方式与.NET中可能导致此问题的标准StmpClient.Send()不同吗?

+1

如果您查看堆栈跟踪,它看起来像SMTPAppender也只是调用SmtpClient.Send。 –

@Bob Geiger指出我正确的方向Log4net似乎使用完全相同的机制。

事实证明,这是一个Virusscan /垃圾邮件保护问题。只允许通过我们的smtp服务器发送邮件的已定义应用程序。虽然我们的一个程序使用了与log4net相同的后端程序,但应用程序使用不同的文件名,因此可以发送邮件,而另一个程序则不允许发送邮件。我们不得不允许virusscan配置中的特定.exe - 文件。