如果我将mail.Dispose()
放在最后,SendMailAsync()
完成其通话后,是否会始终调用mail.Dispose()
?例如,如果我呼叫Post()
一千次,则应在每封电子邮件发送后调用dispose。
这是我的代码:
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();
}
答案 0 :(得分: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);
}
}
}