Roman
Roman

Reputation: 575

"existing connection was forcibly closed" error on sending email in .NET 2.0

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?

Upvotes: 3

Views: 10873

Answers (5)

Mohammad Imran
Mohammad Imran

Reputation: 125

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
                    }

Upvotes: -1

T.W.R. Cole
T.W.R. Cole

Reputation: 4166

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

Upvotes: 1

MethodMan
MethodMan

Reputation: 18843

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

Upvotes: 0

Jason Meckley
Jason Meckley

Reputation: 7591

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

Upvotes: 3

GLP
GLP

Reputation: 3675

Maybe you should set SmtpClient.EnableSsl to false.

Upvotes: 0

Related Questions