C# how to correctly dispose of an SmtpClient?

后端 未结 6 1123
小鲜肉
小鲜肉 2020-12-05 12:57

VS 2010 code analysis reports the following:

Warning 4 CA2000 : Microsoft.Reliability : In method \'Mailer.SendMessage()\', object \'client\' is not disposed along

6条回答
  •  庸人自扰
    2020-12-05 13:58

    public void SendMessage()
    {
        try
        {
            using (SmtpClient client = new SmtpClient())
            {
                client.Send(Message);
            client.dispose()
    
            }
        }
        finally
        {
            DisposeAttachments(); 
        }
    }
    

提交回复
热议问题