2

Will mail.Dispose() always be called after the SendMailAsync() completes its call if I put mail.Dispose() at the end? For example, if I am calling the Post() a thousand times, the dispose should be called after each email is sent.

Here is my code:

public async Task Post(NotificationData notification)
{
    MailMessage mail = new MailMessage();
    mail.To.Add(new MailAddress(notification.Email));
    mail.Subject = notification.Subject;
    mail.Body = notification.Body;

    using (SmtpClient smtp = new SmtpClient())
    {
        smtp.SendCompleted += new SendCompletedEventHandler(SmtpClient_SendCompleted);
        await smtp.SendMailAsync(mail);
    }

    mail.Dispose();
}
Servy
  • 202,030
  • 26
  • 332
  • 449
Ron T
  • 397
  • 1
  • 4
  • 22
  • 3
    you could wrap tthe mail in another using. – Daniel A. White Feb 22 '17 at 21:32
  • You do not need to call Dispose explicitly, unless mail message has attachments *which I do not see in your code*. – Win Feb 22 '17 at 21:36
  • @DanielA.White I updated the post putting the MailMessage in a using statement and palce the SmtpClient in it. So that should be fine right? – Ron T Feb 22 '17 at 21:37
  • @Win Got it, I will be allowing attachments but this is what I have for now – Ron T Feb 22 '17 at 21:38
  • @Win Generally speaking if it implements `Dispose` it should be called. In this case `MailMessage` disposes of things [other than attachments](http://stackoverflow.com/questions/8477769/why-do-i-need-to-dispose-a-system-net-mail-mailmessage-instance). – Matt Feb 22 '17 at 21:39
  • @RonT If you've answered your question sufficiently, consider writing an answer yourself! – Matt Feb 22 '17 at 21:40
  • Possible duplicate of [C# how to correctly dispose of an SmtpClient?](http://stackoverflow.com/questions/2781103/c-sharp-how-to-correctly-dispose-of-an-smtpclient), [Why do I need to Dispose a System.Net.Mail.MailMessage instance?](http://stackoverflow.com/questions/8477769/why-do-i-need-to-dispose-a-system-net-mail-mailmessage-instance) – Win Feb 22 '17 at 21:42
  • @Win my question was to confirm if the mailMessage gets disposed after the email is sent. I wasn't sure if that was the case because I am sending the emails asynchronously. For example, what if the mailMessage gets disposed before the email actually gets sent out. – Ron T Feb 22 '17 at 21:45
  • @RonT since you are awaiting on smtp.SendMailAsync(mail), the next lines will not be executed until that operation is completed. Your current execution thread will be free while the IO operation of sending mail occurs, but the execution of that stack will not resume until SendMailAsync completes due to the await. So your mail message will not get disposed until it is sent with the current pattern. – davidallyoung Feb 22 '17 at 22:24
  • Awesome, good to know and thank you for the clarification @davidallyoung – Ron T Feb 22 '17 at 22:33
  • @RonT: Yes, `Dispose` will be called after `SendMailAsync`, unless some code threw an exception. This is why `using` is preferred over `Dispose`. – Stephen Cleary Feb 23 '17 at 13:55

1 Answers1

1
public async Task Post(NotificationData notification)
{
    using (MailMessage mail = new MailMessage())
    {
        mail.To.Add(new MailAddress(notification.Email));
        mail.Subject = notification.Subject;
        mail.Body = notification.Body;

        using (SmtpClient smtp = new SmtpClient())
        {
            smtp.SendCompleted += new SendCompletedEventHandler(SmtpClient_SendCompleted);
            await smtp.SendMailAsync(mail);
        }
    }
}
Servy
  • 202,030
  • 26
  • 332
  • 449
Ron T
  • 397
  • 1
  • 4
  • 22