Click here to Skip to main content
Click here to Skip to main content

Resolving an SmtpException stating 'Too many messages for this session'

, 1 Oct 2012
Rate this:
Please Sign up or sign in to vote.
Have you ever noticed an exception being thrown by your application stating something like the following:System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: #4.x.2 Too many messages for this sessionThis has been an issue since early versions o

Have you ever noticed an exception being thrown by your application stating something like the following:

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:

  1. 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.
  2. If you are using older versions of the framework (.NET Framework 3.5 or earlier), you can set the MaxIdleTime property to 0 and the ConnectionLimit to 1 on the SmtpClient’s ServicePoint object. For example, your code may look like the following:
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jason N. Gaylord

United States United States
No Biography provided

Comments and Discussions

 
GeneralThanks ! PinmemberMarc-Andre Gauthier29-Jan-13 7:31 
I've seen lots of answers about this but this one is complete. It fixed the problem on my side. Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 1 Oct 2012
Article Copyright 2012 by Jason N. Gaylord
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid