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

时间:2011-12-20 19:53:51

标签: c# asp.net windows sendmail smtpclient

我有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 
}

}

可能导致什么原因?

5 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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

如上所述,也许你正在达到一些你不知道的限制;或者,也许远程服务器特别繁忙并关闭连接(并且没有为该情况返回正确的错误代码)。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您在哪里分配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("From@online.microsoft.com");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
smtp.Send(message);

答案 4 :(得分:-1)

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

例如 -

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