windows 在 .NET 2.0 中发送电子邮件时出现“现有连接被强行关闭”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8581112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"existing connection was forcibly closed" error on sending email in .NET 2.0
提问by Roman
I have asp.net web-site. I'm sending email through SMTPClient
. And from time to time I've gotten this error:
我有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).
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?
什么原因会导致它?
回答by Jason Meckley
MailMessage is disposable. you could try wrapping it in a using statement.
MailMessage 是一次性的。您可以尝试将其包装在 using 语句中。
using(var message = new MailMessage {Subject ="...", Body="..."})
{
message.To.Add("email address");
new SmtpClient().Send(message)
}
回答by T.W.R. Cole
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).
正如上面所建议的,也许您正在达到一些您不知道的限制;或者,也许远程服务器特别忙并关闭了连接(并且不返回正确的错误代码)。
回答by GLP
Maybe you should set SmtpClient.EnableSsl to false.
也许您应该将 SmtpClient.EnableSsl 设置为 false。
回答by MethodMan
where are you assigning the SMTP Client's Host name ...?
您在哪里分配 SMTP 客户端的主机名...?
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..
如果您将代码更改为使用以下内容,不确定是否需要网络凭据我不熟悉您的规格,但是我使用我的东西测试的这段代码刚刚尝试过,并且可以正常工作..
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("youremailAddress");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("[email protected]");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
smtp.Send(message);
回答by Mohammad Imran
SMTPClient class implement IDisposable interface. whenever Type implements IDisposable then you should use Using(){}Statement.
SMTPClient 类实现 IDisposable 接口。每当 Type 实现 IDisposable 时,您就应该使用Using(){}语句。
eg-
例如-
using (SmtpClient SmtpServer = new SmtpClient(smtpConfig.SmtpMailServer))
{
//your code
}