Have you ever noticed an exception being thrown by your application stating something like the following:
1 2 System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: #4.x.2 Too many messages for this session
This has been an issue since early versions of the
System.Net.Mail namespace. The
SmtpServer object never included a
Dispose() method that properly shutdown the connection to the server. So, even if you are creating new objects, the GC never disposed of the original thus causing this exception.
There are two workarounds for this exception as document on the Connect website:
Upgrade to the .NET Framework 4.0 or later. This version of the framework now includes a
Dispose()method that properly closes the connection to the server. Anytime you are connecting to the server to send a message, you should dispose the object afterwards.
If you are using older versions of the framework (.NET Framework 3.5 or earlier), you can set the
MaxIdleTimeproperty to 0 and the
ConnectionLimitto 1 on the
ServicePointobject. For example, your code may look like the following:
1 2 3 4 5 6 7 8 var client = new SmtpClient("hostname"); client.ServicePoint.MaxIdleTime = 0; client.ServicePoint.ConnectionLimit = 1; ... client.Send(new MailMessage(...)); // at this point, the connection will get closed // since the ServicePoint idle time is now 0.
Hope this helps to solve any issues you’ve had with this.