“现有连接被强制关闭”在 .NET 2.0 中发送电子邮件时出错
我有 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
}
}
什么原因会导致它?
I have asp.net web-site. I'm sending email through SMTPClient
. And from time to time I've gotten this error:
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).
Code:
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
}
}
What reasons can cause it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
MailMessage 是一次性的。您可以尝试将其包装在 using 语句中。
MailMessage is disposable. you could try wrapping it in a using statement.
我确实相信它与您发布的实际代码关系不大,而与您所连接的服务器所解释的请求的数量和内容有关。强制关闭连接是一种故意行为,而不是随机错误,这意味着服务器是根据满足某些条件来选择这样做的。
正如上面所建议的,也许您正在达到一些您不知道的极限;或者,远程服务器可能特别繁忙并关闭连接(并且不会返回针对这种情况的正确错误代码)。
I do believe that it has little to do with the actual code that you've posted, and everything to do with the volume and content of your requests, as interpreted by the server you're connecting to. Forcibly closing a connection is a willful act and not a random error, which means the server is choosing to do so based on some conditions being satisfied.
As has been suggested above, perhaps you are hitting some limit unknown to you; or, perhaps the remote server is particularly busy and closes connections (and doesn't return the proper error code for that case).
也许您应该将 SmtpClient.EnableSsl 设置为 false。
Maybe you should set SmtpClient.EnableSsl to false.
您在哪里分配 SMTP 客户端的主机名...?
如果您更改代码以使用以下内容,不确定是否需要网络凭据,我不熟悉您的规格,但我使用我的东西测试的这段代码只是尝试了一下,它有效。
where are you assigning the SMTP Client's Host name ...?
what if you changed your code to use the following not sure if Network Credentials are needed I am not familiar with your specs but this code I tested using my stuff just tried it and it works..
SMTPClient 类实现 IDisposable 接口。
每当 Type 实现 IDisposable 时,您都应该使用 Using(){} 语句。
例如-
SMTPClient class implement IDisposable interface.
whenever Type implements IDisposable then you should use Using(){} Statement.
eg-