SmtpClient in .net 2.0 does not close connection to mail server

SMTPClient class provided in the .net framework 2.0 is buggy and NEVER sends a QUIT command to the SMTP Server. This issue has been reported to Microsoft Connect.

As per the SMTP protocol once the communication is done by the client(asp.net web, windows application or any other client app) with email server, it has to send QUIT command in the end to close the connection with Mail Server.

SmtpClient in .net framework 2.0 doesn’t have dispose method which can release connection with Mail server and free up any resources used. That means if your application is sending too many emails using smtpclient.send function, it oftens reach to maximum number of allowed connections with mail server and results in one of the following exceptions:

Service not available, closing transmission channel. The server response was: #4.x.2 Too many messages for this session

System.Net.WebException: Unable to connect to the remote server

System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

It is fixed in .Net Framework 4.0

Good news is that, the .Net 4.0 now supports dispose method in SmtpClient class. according to MSDN: It sends a QUIT command to the SMTP server, and gracefully ends the TCP connection to releases all resources used by the current instance of the SmtpClient class.

Comments (2) -

  • Steve Johnson
    This is useful information.
    Dispose method has been added in .Net 4.0 which sends QUIT command to Mail server
  • saurabh
    how we can send QUIT command to the SMTP Server in before 4.0 verisons

Add comment

Loading