我们在调用特定MVC控制器方法时发送电子邮件。代码如下。在浏览器内对AJAX响应时间进行基准测试时,我们发现client.SendAsync(mail, null)
和client.Send(mail)
实现大约需要相同的时间,即完全不发送邮件的时间为1.8秒。
public ActionResult jsonPosted(int? id, SomeDataClass data)
{
...
// NOTE: remode 'using' statement for Async calls
// else no email ever sees light
using (SmtpClient client = new SmtpClient())
{
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("name@server.com", "password");
client.Port = 587;
client.Host = "smtp.server.com";
client.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("admin@server.com");
mail.To.Add("sales@server.com");
mail.Subject = "Coming soon: user " + newUser.Email + " registered on list";
mail.Body = "Email address is : " + newUser.Email + "\n";
mail.Body += "Date of signup is : " + newUser.SignupTime.ToString();
try
{
// Both take about 1.9 seconds ?!?!
//client.SendAsync(mail, null);
client.Send(mail);
}
catch (SmtpException e)
{
// bad smtp, no mail for you
}
}
...
return Json(dataIndependentOfEmail);
}
问题:
using
在异步模式下处理SmtpClient
和MailMessage
等类?答案 0 :(得分:0)