当我们的应用程序发生任何错误时,我有如下方法发送电子邮件。[是的。通过邮件发送错误是一个单独的讨论]
public void EmailErrorDetails()
{
string strBodyMessage = string.Empty;
strBodyMessage = GetEmailBodyMessage();
if (strBodyMessage != String.Empty)
{
MailMessage emailMessage = new MailMessage();
emailMessage.From = new MailAddress(Constants.MailFrom);
emailMessage.To.Add(Constants.MailTo);
emailMessage.Subject = Constants.EmailSubject;
emailMessage.IsBodyHtml = true;
emailMessage.Body = string.Format(strBodyMessage);
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = Constants.EmailHostAddress;
client.Port = Convert.ToInt32(Constants.EmailPort);
client.Credentials = new NetworkCredential(Constants.MailFrom, Constants.MailFromPassword);
client.Send(emailMessage);
}
}
我想让这个方法在后台异步运行,并继续执行。
我读到 [client.Send] 本身使用异步调用。那么使上述方法调用异步方式有什么好处,如果是的话,我该如何实现呢?