当我使用 SmtpClient 的 SendAsync 发送电子邮件时,如何smtpclient
正确处置实例?
比方说:
MailMessage mail = new System.Net.Mail.MailMessage()
{
Body = MailBody.ToString(),
IsBodyHtml = true,
From = new MailAddress(FromEmail, FromEmailTitle),
Subject = MailSubject
};
mail.To.Add(new MailAddress(i.Email, ""));
SmtpClient sc = new SmtpClient(SmtpServerAddress);
//Add SendAsyncCallback to SendCompleted
sc.SendCompleted += new SendCompletedEventHandler(SendAsyncCallback);
//using SmtpClient to make async send (Should I pass sc or mail into SendAsyncCallback?)
sc.SendAsync(mail, sc);
在SendAsyncCallback
方法中,我应该调用sc.Dispose()
,还是mail.Dispose()
?
我查看了 MSDN 文档,其中一个示例调用 MailMessage.Dispose(),但是这个 dispose 方法也会释放SmtpClient
实例吗?