views:

696

answers:

5

We've got a few pages in our web systems that use the .net system.net.mail control to send emails. The thing has been working great, except it's now starting to look like the smptclient class may not actually be disconnecting from the server, such that the SMTP server leaves that connection open, and we ended up maxing out the number of connections we were allowed to have open at a time on the SMTP server, despite only sending one email at a time.

(For the record, this is a .net 2.0 asp.net application written in VB, and we're fairly confident this isn't some kind of security / virus / spam passthrough situation.)

Google and MSDN didn't turn up anything conclusive, but just enough heresy in blog entries to confirm that we might not be hallucinating.

Any one else out there ever have this problem? (And manage to fix it?)

Of course, if it does work fine, and we are hallucinating, that would be nice to know too. ;)

+1  A: 

Are you properly disposing of your MailMessage once it has been sent? I've never seen any indication that SmtpClient wasn't closing it's connection though

Slace
Yes, we're disposing of the MailMessage object. Thanks, though.
Electrons_Ahoy
+1  A: 

Did you check the StmpDeliveryMthod property of the StmpClient class?

Michael Kniskern
+1  A: 

A handy tool for this type of 'am I connected' question is TcpView ( \\live.sysinternals.com\Tools\Tcpview.exe ) from SysInternals (now Microsoft)

MotoWilliams
+1  A: 

Make sure these are set on the mail server (assuming windows) -

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters] "TcpTimedWaitDelay"=dword:0000001e "MaxFreeTcbs"=dword:000007d0 "MaxUserPort"=dword:0000fffe

audiphilth
+3  A: 

The problem of emails not being sent right away with SmtpClient is because under some conditions, it does not send the SMTP command 'QUIT' when it should (i.e. it doesn't disconnect properly). I have used the following code to force a disconnect successfully in the past:

var smtp = new SmtpClient();
smtp.ServicePoint.MaxIdleTime = 1;
smtp.ServicePoint.ConnectionLimit = 1;
smtp.Send(message);

This forces the smtpclient object to disconnect at the earliest possible time. Also, make sure your smtpclient instance falls out of scope fairly quickly (i.e. do not store a static reference to it somewhere).

Chris
Oh wow. That's useful to know. I think I just gave up and decided on setting it to the IIS pickup directory.
Min
I recommend against using this method. If you do, check the CPU usage of your server and see if it reaches 100% of one CPU.
BobbyShaftoe