【发布时间】:2017-01-23 09:46:49
【问题描述】:
这就是我需要发送电子邮件时给我的错误。但是自从给我的错误是这样的:
此时无法启动异步操作。异步 操作只能在异步处理程序中启动或 模块或页面生命周期中的某些事件期间。如果这 执行页面时发生异常,请确保该页面是 标记为 。此异常也可能表明 尝试调用通常不受支持的“async void”方法 在 ASP.NET 请求处理中。相反,异步方法 应该返回一个任务,调用者应该等待它。
我从 MVC 开始积累,并使用类来跟踪页面的 ie 区域。我使用 SendAsync 的原因正是它发送电子邮件等速度更快。
此错误仅在我尝试向用户发送电子邮件时发生。
public static void NewPassword(string mail, string name, string password)
{
MailDefinition oMailDefinition = new MailDefinition();
oMailDefinition.BodyFileName = "~/MailList/emailskabelon/NewPassword.html";
oMailDefinition.From = FromMail;
Dictionary<string, string> oReplacements = new Dictionary<string, string>();
oReplacements.Add("<<navn>>", name);
oReplacements.Add("<<password>>", password);
System.Net.Mail.MailMessage oMailMessage = oMailDefinition.CreateMailMessage(mail, oReplacements, new LiteralControl());
oMailMessage.Subject = NewpasswordTitle + WebsiteName;
oMailMessage.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(AzureApi);
System.Net.NetworkCredential netcred = new System.Net.NetworkCredential(AzureName, AzurePassword);
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = netcred;
smtp.Port = Convert.ToInt32("25");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
using (var smtpClient = new SmtpClient())
{
smtp.SendAsync(oMailMessage, null);
}
}
我试过这样做:
public static async NewPassword(string mail, string name, string password)
{
....
using (var smtpClient = new SmtpClient())
{
await smtp.SendAsync(oMailMessage, null);
}
【问题讨论】:
-
据我了解,异步方法需要一个返回类型——无论是 void 还是其他类型。
标签: c# asp.net email asynchronous