“现有连接被强制关闭”在 .NET 2.0 中发送电子邮件时出错

发布于 2024-12-22 01:38:54 字数 2384 浏览 0 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

贪恋 2024-12-29 01:38:54

MailMessage 是一次性的。您可以尝试将其包装在 using 语句中。

using(var message = new MailMessage {Subject ="...", Body="..."})
{
   message.To.Add("email address");
   new SmtpClient().Send(message)
}

MailMessage is disposable. you could try wrapping it in a using statement.

using(var message = new MailMessage {Subject ="...", Body="..."})
{
   message.To.Add("email address");
   new SmtpClient().Send(message)
}
疧_╮線 2024-12-29 01:38:54

我确实相信它与您发布的实际代码关系不大,而与您所连接的服务器所解释的请求的数量和内容有关。强制关闭连接是一种故意行为,而不是随机错误,这意味着服务器是根据满足某些条件来选择这样做的。

正如上面所建议的,也许您正在达到一些您不知道的极限;或者,远程服务器可能特别繁忙并关闭连接(并且不会返回针对这种情况的正确错误代码)。

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).

云仙小弟 2024-12-29 01:38:54

也许您应该将 SmtpClient.EnableSsl 设置为 false。

Maybe you should set SmtpClient.EnableSsl to false.

爱本泡沫多脆弱 2024-12-29 01:38:54

您在哪里分配 SMTP 客户端的主机名...?

如果您更改代码以使用以下内容,不确定是否需要网络凭据,我不熟悉您的规格,但我使用我的东西测试的这段代码只是尝试了一下,它有效。

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);

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..

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);
乞讨 2024-12-29 01:38:54

SMTPClient 类实现 IDisposable 接口。
每当 Type 实现 IDisposable 时,您都应该使用 Using(){} 语句。

例如-

using (SmtpClient SmtpServer = new SmtpClient(smtpConfig.SmtpMailServer))
                    {
                       //your code
                    }

SMTPClient class implement IDisposable interface.
whenever Type implements IDisposable then you should use Using(){} Statement.

eg-

using (SmtpClient SmtpServer = new SmtpClient(smtpConfig.SmtpMailServer))
                    {
                       //your code
                    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文