Dispose SmtpClient in SendComplete?

后端 未结 2 1260
孤独总比滥情好
孤独总比滥情好 2021-01-11 16:47

When I use SmtpClient\'s SendAsync to send email, how do I dispose the smtpclient instance correctly?

Let\'s say:


MailMessage mail = new          


        
2条回答
  •  忘掉有多难
    2021-01-11 17:02

    You should dispose both the MailMessage and the SmtpClient in SendAsyncCallback.

    Disposing the MailMessage will not dispose the SmtpClient automatically (because you might want to send two messages with the same SmtpClient, and you wouldn't want the client to be disposed as soon as you disposed the first message).

提交回复
热议问题