Answered by:
Trying to get Password Reset emails to be sent

Question
-
User269881539 posted
I have setup ASP.net identity but I cannot get the password reset emails to be sent. I have tried to plumb in my email sending code (which works for other processes) but I don't even think it's being called :
My controller code :
// POST: /Account/ForgotPassword [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(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"); } // Send an email with this link var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("AppName"); UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation")); 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); }
My adapted IdentityConfig.cs file :
public class EmailService : IIdentityMessageService { public Task SendAsync(IdentityMessage message) { // Plug in your email service here to send an email. //send message EmailManager eml = new EmailManager(); eml.Email = message.Destination; eml.Subject = message.Subject; eml.Body = message.Body; eml.IsHtml = true; eml.SendEmail(); return Task.FromResult(0); } }
When debugging it doesn't seem to drop into that task at all - I can't see where UserManager.SendEmailAsync ties into that?
Thursday, September 15, 2016 1:51 PM
Answers
-
User269881539 posted
Got it working - was missing this line of code in my controller action :
UserManager.EmailService = new EmailService(); await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 16, 2016 4:13 PM
All replies
-
User1724605321 posted
Hi chilluk ,
Please refer to below tutorials , you will get code sample step by step :
Best Regards,
Nan Yu
Friday, September 16, 2016 6:44 AM -
User269881539 posted
I have looked through those and it doesn't seem a lot different to what I am doing.
I don't want email confirmation of the account in the first place so I have removed that email part - it's an ecommerce site and customer won't wait for that to continue. Would it be an issue if the account hasn't yet been confirmed?
I am using local SMTP server which works for other emails.
I can't see the code in Task SendAsync getting called at all - how does await UserManager.SendEmailAsync() this relate to that anyway?
Friday, September 16, 2016 8:53 AM -
User1724605321 posted
Hi chilluk ,
Please change your code like :
public class EmailService : IIdentityMessageService { public Task SendAsync(IdentityMessage message) { SmtpClient client = new SmtpClient(); return client.SendMailAsync(ConfigurationManager.AppSettings["SupportEmailAddr"], message.Destination, message.Subject, message.Body); } }
Best Regards,
Nan Yu
Friday, September 16, 2016 9:05 AM -
User269881539 posted
OK this is my code now :
public class EmailService : IIdentityMessageService { public Task SendAsync(IdentityMessage message) { // Plug in your email service here to send an email. SmtpClient client = new SmtpClient(AppConstants.SmtpServer); return client.SendMailAsync("xxx@xxxx.com", message.Destination, message.Subject, message.Body); } }
Still nothing - I can see it's not even trying by the SMTP logs - I also went in and changed all records in AspNetUsers to EmailConfirmed = true
It looks like the mailing code is not even running - I assume this line in my AccountController is supposed to be calling it ?
await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
But I can't see the correlation between that and the SendAsync task in IdentityConfig.cs?
Friday, September 16, 2016 10:26 AM -
User269881539 posted
In that second example where would this code go?
void sendMail(Message message)
I had to add this code to get the Controller Action working
var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("TooledUp"); UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));
Would that be causing an issue?
Friday, September 16, 2016 2:40 PM -
User269881539 posted
Got it working - was missing this line of code in my controller action :
UserManager.EmailService = new EmailService(); await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 16, 2016 4:13 PM