【发布时间】:2011-12-20 19:53:50
【问题描述】:
我有 asp.net 网站。我正在通过SMTPClient 发送电子邮件。我不时收到此错误:
InnerException = 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 ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
at System.Net.Mail.DataStopCommand.Send(SmtpConnection conn)
at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args)
at System.Net.ClosableStream.Close()
at System.Net.Mail.MailWriter.Close()
at System.Net.Mail.SmtpClient.Send(MailMessage message).
代码:
SmtpClient smtpClient = new SmtpClient(ConfigurationManager.AppSettings["smtpserverwwwpo"]);
try
{
NetworkCredential basicCredential =
new NetworkCredential(ConfigurationManager.AppSettings["smtp_user_name"], ConfigurationManager.AppSettings["smtp_password"]);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
}
catch
{
smtpClient.UseDefaultCredentials = true;
}
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.Subject = subject;
msg.To.Add(new MailAddress(someEmail));
msg.From = new MailAddress(ConfigurationManager.AppSettings["from_email_address"]);
msg.Headers.Add("Content-Type", "text/html");
msg.BodyEncoding = System.Text.Encoding.UTF8;
try{
smtpClient.Send(msg);
}
catch(Exception ex)
{
//write log
}
}
什么原因会导致呢?
【问题讨论】:
-
看起来 SMTP 服务器关闭了连接。也许检查服务器的日志?
-
您是使用套接字还是使用 smtp / System.Net.Mail 发送电子邮件?您有什么代码请发布.. 通过 .NET 发送电子邮件相当简单直接
-
您可能被远程主机上的垃圾邮件预防机制踢了。您生成电子邮件的速度有多快?您要发送到哪种邮件服务器(Exchange、sendmail 等)?
-
您可以通过配置文件中的 system.net.mail 而不是 appsettings 配置 SmtpClient 来清理大部分代码。这对您的问题没有任何影响,但会使代码更清晰。
-
我也看到了这个,关键短语(从我的角度来看)是“不时”。周五,一名应用用户经历了 24 次发起电子邮件的过程,其中 3 次失败。我习惯于击球 1.000,但 0.875 已经足够好,我自己的代码似乎得到了肯定。 (这开始于几个月前,在 .NET 2.0 上,我已将应用程序更新到 .NET 3.5,但交易相同。)
标签: c# asp.net windows sendmail smtpclient