【发布时间】:2011-10-19 14:38:06
【问题描述】:
我有一个发送简单电子邮件的操作:
[HttpPost, ActionName("Index")]
public ActionResult IndexPost(ContactForm contactForm)
{
if (ModelState.IsValid)
{
new EmailService().SendAsync(contactForm.Email, contactForm.Name, contactForm.Subject, contactForm.Body, true);
return RedirectToAction(MVC.Contact.Success());
}
return View(contactForm);
}
还有一个电子邮件服务:
public void SendAsync(string fromEmail, string fromName, string subject, string body, bool isBodyHtml)
{
MailMessage mailMessage....
....
SmtpClient client = new SmtpClient(settingRepository.SmtpAddress, settingRepository.SmtpPort);
client.EnableSsl = settingRepository.SmtpSsl;
client.Credentials = new NetworkCredential(settingRepository.SmtpUserName, settingRepository.SmtpPassword);
client.SendCompleted += client_SendCompleted;
client.SendAsync(mailMessage, Tuple.Create(client, mailMessage));
}
private void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Tuple<SmtpClient, MailMessage> data = (Tuple<SmtpClient, MailMessage>)e.UserState;
data.Item1.Dispose();
data.Item2.Dispose();
if (e.Error != null)
{
}
}
当我发送电子邮件时,我使用的是 Async 方法,然后我的方法 SendAsync 立即返回,然后调用 RedirectToAction。但是在 client_SendCompleted 完成之前,ASP.NET 不会发送响应(在本例中为重定向)。
这是我想要理解的:
在 Visual Studio 调试器中观察执行时,SendAsync 立即返回(并调用 RedirectToAction),但在发送电子邮件之前浏览器中什么都没有发生?
如果我在 client_SendCompleted 中设置断点,客户端会一直加载...直到我在调试器中按 F5。
【问题讨论】:
标签: asp.net-mvc-3 email smtp smtpclient