Search

SmtpClient does not close session after sending message by theBoringCoder

Closed
as Not Reproducible Help for as Not Reproducible

11
0
Sign in
to vote
Type: Bug
ID: 337557
Opened: 4/9/2008 3:46:24 PM
Access Restriction: Public
1
Workaround(s)
8
User(s) can reproduce this bug
My ASP.NET website sends emails throughout the day for various reasons.

Each time an email is to be sent, an instance of a class I wrote is created on the web server. This class builds the MailMessage, creates an instance of SMTPClient, and calls the Send method.

During periods of heavy load, SMTPExceptions are sporadically (no discernable pattern) thrown with the following message:

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

My SMTP Server admin has responded that our SMTP Server DOES have a limit on the number of messages that can be sent by a single session.

The System.Net.Mail.SmtpClient class has no close or dispose method, so I have no control over the closing of my SMTP Session.
Details (expand)
Product Language
English

Version

.NET Framework 2.0 Service Pack 1
Operating System
Windows XP Professional
Operating System Language
English
Steps to Reproduce
Setup an SMTP Server, and set an arbitrary limit on the number of messages that can be sent in a snigle session. Then, write some code that calls the following function more times than the arbitrary limit.

Private Shared Function _send(ByVal serverName As String, _
ByVal message As System.Net.Mail.MailMessage) As Boolean
Dim client As System.Net.Mail.SmtpClient = Nothing

Dim result As Boolean = False

client = New System.Net.Mail.SmtpClient(serverName)

client.Send(message)

result = True

client = Nothing

Return result
End Function
Actual Results
SMTPExceptions are thrown
Expected Results
SMTPExceptions will be thrown
TAP Code (if applicable)
 
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey. [Details]

 

File Attachments
0 attachments
Sign in to post a comment.
Posted by theBoringCoder at work on 1/6/2011 at 10:38 AM
Closed as Not Reproducable? Really? 7 of us could reproduce this bug...but the product team at Microsoft couldn't.
Posted by theBoringCoder on 3/10/2010 at 10:46 PM
Fantastic! Thanks for the fix.
Posted by Microsoft on 3/10/2010 at 9:44 AM
A new SmtpClient.Dispose() method was added to the .NET 4.0 Framework. Using this method will close the connection to the server.

Thank you,

Network Class Library Team
Posted by theBoringCoder at work on 12/9/2009 at 9:17 AM
Andy, it has been my experience that, yes, even if you are creating a new instance of the SmtpClient every time, it still uses the same unerlying session.
Posted by theBoringCoder at work on 12/9/2009 at 9:16 AM
I'm glad I'm not the only person having this problem.
Posted by Andy Stephens on 12/7/2009 at 3:55 AM
I'm also suffering from this problem and a solution would be much appreciated.

I'm creating a new instance of SmtpClient for each mesage I send - would this still result in the same underlying connection/session being used?
Posted by JTango on 9/20/2009 at 7:59 PM
as theBoringCoder pointed out, this workaround does not in fact work. Are we likely to see a resolution to this issue any time soon?
Posted by theBoringCoder on 6/30/2009 at 3:00 PM
Man, I really hate to be a downer, but...I tried the suggested work-around:

Smtp client = new SmtpClient("hostname");
client.ServicePoint.MaxIdleTime = 0;
client.ServicePoint.ConnectionLimit = 1;
...
client.Send(new MailMessage(...));

And now the error I receive alternates between:

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

and

"Service not available, closing transmission channel. The server response was: Exceeded allowable connection time, disconnecting."
Posted by theBoringCoder on 6/30/2009 at 8:58 AM
Hi,

Thank you for the information on the workaround. I know you guys are all very busy, and I appreciate the attention.

If I may be so bold, I'd like to make a suggestion: it'd be nice if we had a property that would allow us to set the number of messages to send using the same session. In my case, this would allow me to tailor my app to be in accordance with my server management's 10-emails-per-session policy. That way I wouldn't be closing and opening the connection/session for every message I send.
Posted by Microsoft on 6/30/2009 at 8:33 AM
The SmtpClient class currently does not have a Close() or Dispose() method. However, you can change the default idle timeout of the ServicePoint so that the connection is dropped after each email is sent.

For example:

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

We are considering IDisposable() and Close() semantics for a future version of the Framework.

Thank you,

Network Class Library Team
Posted by Phara0n on 4/2/2009 at 12:43 AM
No, a lot of developers are facing this issue and have to use alternatives to SmtpClient class
Posted by Microsoft on 3/21/2009 at 9:42 AM
Thank you for the feedback on this issue. We have assigned this issue to the product team responsible for the the SmtpClient class in the .NET Framework.    They are reviewing the issue and will provide feedback after review.

Network Class Library Team
Posted by theBoringCoder on 3/9/2009 at 10:33 AM
Hi, some attention from Microsoft on this issue would be great.
Posted by theBoringCoder on 3/9/2009 at 10:32 AM
Am I the only person this happens to?
Posted by theBoringCoder on 12/2/2008 at 11:15 AM
Hi. Anyone have a workaround, solution, or correction for this?
Sign in to post a workaround.
Posted by theBoringCoder on 3/9/2009 at 10:31 AM
The workaround is to catch the SMTPException, and re-send the message as the exception appears to force the underlying SMTP session to close.