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

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

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.

About Author

Kiran Beladiya

Co-Founder

Kiran Beladiya is the co-founder of The One Technologies. He plays a key role in managing the entire project lifecycle, from discussing ideas with clients to overseeing successful releases. Deeply passionate about technology and creativity, he is also an avid writer who continues to nurture and refine his writing skills despite a demanding schedule. Through his work and writing, Kiran Beladiya shares practical insights drawn from real-world experience.

Certified By