我嘗試使用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()不同嗎?
如果您查看堆棧跟蹤,它看起來像SMTPAppender也只是調用SmtpClient.Send。 –