C# 异步电子邮件发送isn';不要返回视图
我的Asp.net网站上的忘记密码电子邮件链接有问题 基本上,一切正常,它发送一封电子邮件到帐户,密码可以重置,但我得到一个404错误,而不是返回正确的页面C# 异步电子邮件发送isn';不要返回视图,c#,asp.net,asp.net-mvc,email,C#,Asp.net,Asp.net Mvc,Email,我的Asp.net网站上的忘记密码电子邮件链接有问题 基本上,一切正常,它发送一封电子邮件到帐户,密码可以重置,但我得到一个404错误,而不是返回正确的页面 public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByEmailAsy
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByEmailAsync(model.Email);
if (user == null) // || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
{
// Don't reveal that the user does not exist or is not confirmed
return View("ForgotPasswordConfirmation");
}
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("ForgotPasswordConfirmation", "Account");
}
// If we got this far, something failed, redisplay form
return View(model);
}
在“电子邮件”部分,您应该会看到以下错误:“异步操作仍挂起时,异步模块或处理程序已完成”。我相信你得到404是因为可能没有找到错误页面 你可以试试下面的方法
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var mailMessage = new MailMessage("Email here",
message.Destination,
message.Subject,
message.Body
);
var client = new SmtpClient();
return client.SendMailAsync(mailMessage);
}
或者使用等待/异步方式
public async Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var mailMessage = new MailMessage("Email here",
message.Destination,
message.Subject,
message.Body
);
var client = new SmtpClient();
await client.SendMailAsync(mailMessage);
}
当您收到404时,我怀疑您发送电子邮件的呼叫失败,它试图显示错误消息,但未能找到错误视图。我会检查设置是否正确电子邮件每次都能完美发送?能否显示您的公共任务SendAsync(IdentityMessage message)实现?我已将其添加到原始代码中。谢谢你,先生,你是一个传奇:-)非常感谢
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var mailMessage = new MailMessage("Email here",
message.Destination,
message.Subject,
message.Body
);
var client = new SmtpClient();
return client.SendMailAsync(mailMessage);
}
public async Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var mailMessage = new MailMessage("Email here",
message.Destination,
message.Subject,
message.Body
);
var client = new SmtpClient();
await client.SendMailAsync(mailMessage);
}