Question

I am using SmtpClient to send mail. How can I dispose SmtpClient. I am using .NET 3.5.

 try
     {
         smtpClient.Send(message);
         Log.WriteSpecialLog("smtpClient mail sending try success", "");
     }
      catch (Exception ee)
     {
         Log.WriteSpecialLog("smtpClient mail sending try Failed : " + ee.ToString(), "");
         return false;
     }
Was it helpful?

Solution

For .NET 3.5, not being able to proper dispose the SmtpClient is a know issue. http://connect.microsoft.com/VisualStudio/feedback/details/146711/smtp-never-sends-the-quit-command

Your question is similar to this one: Properly disposing resources used by SmtpClient

The issue has been fixed in .NET 4.0 http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.100).aspx and for this (and many other reasson) it would be advisible to update the .NET Framework (althought I personally know that sometimes upgrading is an issue).

But, if you really want to dispose the SmtpClient, you can make the class disposable yourself. But I don't know how well this goes for SmtpClient. http://forums.asp.net/t/383218.aspx/1 http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx

As a side note, I would advise you to handle the catch SmtpFailedRecipientsException, not treat exceptions generic. The SmtpStatusCode in the InnerException gives you important informations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top