c# - 异步电子邮件发送未返回 View

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

我的 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>");

如果没有这一行,它会返回正确的 View ,但显然电子邮件不会发送。 我已经调试并逐步检查了它,但找不到任何错误。

有没有人遇到过这个?

提前致谢

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

编辑:身份信息

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 是因为可能没有找到错误页面。

你可以试试下面的方法

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

关于c# - 异步电子邮件发送未返回 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34863802/

相关文章:

c# - 如何在 C# 中获取当前区域设置?

c# - 关闭所有子窗体时退出应用程序

c# - 如何在所谓的事件中找到哪个控件引发了事件?视觉 C#

c# - 使用 C# 解密 Coldfusion 中的加密字符串

c# - 如何为特定路由 Asp.Net MVC 构造我的操作方法

c# - RijndaelManaged - 设置 KeySize 属性有什么作用?

ASP.NET 教程

c# - 如何使用 javascript 与 asp.net viewstate

jquery - 显示错误/警告的最佳方法是在MVC中使其显示或传递自定义属性?

javascript - 添加多个局部 View ,但第一个 View 缺少表单