scottdavidwalker scottdavidwalker - 3 months ago 26
ASP.NET (C#) Question

Async E-mail sending isn't returning a view

I have an issue with the forgot password email link on my Asp.net website.

Basically, everything works fine, it sends an email to the account and the password can get reset but I get a 404 error rather than returning the correct page.

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


I think it's an issue with this line:

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


Without this line, it returns the correct view but obviously the email won't send.
I've debugged and stepped through it but can't find any errors.

Has anyone else encountered this?

Thanks in advance

N.b. if the model is null, it returns the correct view

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

return Task.FromResult(0);
}

Answer

In the email section you should get this error "An asynchronous module or handler completed while an asynchronous operation was still pending". I believe you got 404 is because there might not Error page found.

You can try the following

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

Or using the await/async way

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