我使用等待函数来发送电子邮件。是否有任何解决方案如何在等待await函数完成时显示页面或返回view()。
这是我的代码:
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="751018141c19351218141c195b161a18" rel="noreferrer noopener nofollow">[email protected]</a>",
Password = "paswordEmail"
};
smtp.Credentials = credential;
smtp.Host = "smtp-mail.outlook.com";
smtp.Port = 587;
smtp.EnableSsl = true;
await smtp.SendMailAsync(message); //Here is my await function for sending an email
return RedirectToAction("ThankYou"); //This thank you page. I want to display this html page without waiting for await complete
}
最佳答案
您可以将邮件代码包装在 Task.Run
中而不等待它。
Task.Run(async () => {
using (var smtp = new SmtpClient()) {
var credential = new NetworkCredential {
UserName = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96f3fbf7fffad6f1fbf7fffab8f5f9fb" rel="noreferrer noopener nofollow">[email protected]</a>",
Password = "paswordEmail"
};
smtp.Credentials = credential;
smtp.Host = "smtp-mail.outlook.com";
smtp.Port = 587;
smtp.EnableSsl = true;
await smtp.SendMailAsync(message); //Here is my await function for sending an email
}
});
return RedirectToAction("ThankYou");
关于c# - 如何在MVC中等待await函数时返回 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54300604/