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. 😉
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:
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).