我遇到了.NET SmtpClient
的问题。 Send
方法经常会抛出以下异常:
System.Net.Mail.SmtpException: Failure sending mail.
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
我们平均每小时生成约550封电子邮件(或每分钟约9封电子邮件),质量保证似乎永远不会出现此问题。
每封电子邮件都是这样发送的:
using (var newMessage = new MailMessage { ReplyTo = new MailAddress(supportEmail, "Support") })
{
newMessage.To.Add(recipients);
newMessage.From = new MailAddress(fromEmail, "From Name");
newMessage.Subject = message.Subject;
newMessage.Body = body;
using (var mailHost = new SmtpClient())
{
mailHost.Send(newMessage);
}
}
请注意,我们正在处理所有资源,我们没有使用SSL,而且我们正在加载来自web.config
的Smtp设置,如下所示:
<mailSettings>
<smtp from="validEmail" deliveryMethod="Network" >
<network defaultCredentials ="false" host="dedicatedSmtpServer" port="port"/>
</smtp>
</mailSettings>
我把一些笔记放在一起:
SmtpClient
实例发送。即使在批量发送方案中也是如此。我已经读过,这可能会影响性能,因为套接字没有汇集。但是,当 工作时,性能似乎已足够。SmtpClient
未终止连接的错误已得到修复。答案 0 :(得分:0)
您的代码与此无关。这个特殊错误:
无法从传输连接读取数据:远程主机强行关闭现有连接。
基本上意味着远程端已经终止了套接字。 “强行”只是意味着它以一种不寻常的方式完成,但它仍然是远程端。这可能是由于套接字保持打开的时间长短,所以实现套接字池肯定是个好主意。