Dispose SmtpClient in SendComplete?

后端 未结 2 1248
孤独总比滥情好
孤独总比滥情好 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:10

    This example: from the MSDN Library documentation only closes the Message so I'm going with that in my implementation: SmtpClient.SendAsync Method

    message.Dispose();
    

    I was running into this issue referred to in this question where the send was always being cancelled so I'm removing my using {} statement: SmtpClient.SendAsync Calls are Automatically Cancelled

    Okay, I just tried issuing the message.Dispose() and even that was throwing the error saying it couldn't send the email because of the message being disposed. Possibly because mine is a asp.net mvc app and the example is a console app. In any case the garbage collector should pick up these options once everything falls out of scope...

提交回复
热议问题