【问题标题】:Async E-mail sending isn't returning a view异步电子邮件发送未返回视图
【发布时间】:2016-04-24 03:49:58
【问题描述】:

我对我的 Asp.net 网站上忘记密码的电子邮件链接有疑问。

基本上,一切正常,它会向帐户发送一封电子邮件,并且可以重置密码,但我收到 404 错误,而不是返回正确的页面。

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);
}

我认为这行有问题:

await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");

没有这一行,它会返回正确的视图,但显然电子邮件不会发送。 我已经调试并逐步完成,但找不到任何错误。

有没有其他人遇到过这种情况?

提前致谢

注意如果模型为空,则返回正确的视图

编辑:身份信息

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();
    client.SendAsync(mailMessage, null);

    return Task.FromResult(0);
}

【问题讨论】:

  • 当您收到 404 时,我怀疑您发送电子邮件的调用失败,它尝试显示错误消息但未能找到错误视图。我会检查是否设置正确
  • 邮件每次都能完美发送?
  • 你能展示你的公共任务 SendAsync(IdentityMessage message) 实现吗?
  • 我已将其添加到原始代码中。谢谢

标签: c# asp.net asp.net-mvc email


【解决方案1】:

在电子邮件部分中,您应该会收到此错误消息“异步模块或处理程序已完成,而异步操作仍处于挂起状态”。我相信你得到 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);
    }

或者使用await/async方式

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);
        }

【讨论】:

  • 你先生,是一个传奇 :-) 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 2017-07-13
  • 2011-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多