How to dispose an SmtpClient in .NET 3.5?

孤街浪徒 提交于 2019-12-11 03:08:32

问题


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;
     }

回答1:


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.



来源:https://stackoverflow.com/questions/18267644/how-to-dispose-an-smtpclient-in-net-3-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!