Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserManager SendEmailAsync No Email Sent

Tags:

c#

asp.net-mvc

I am using the following code to try to send an email asynchronously, but no email is sent and I am not sure what is being done incorrectly. I have also added the 2nd segment of code in the web.config for the emailing protocol.

SendEmailAsync code

await UserManager.SendEmailAsync(username.Id, "MTSS-B: Forgot Password", "Here is your new password. Please go back to the MTSS-B web tool and sign in. You will be prompted to create your own password.<br/><br/>" + tmpPass + "<br/><br/>MTSS-B Administrator"); 

Web.config code

<system.net> <mailSettings>   <smtp>     <network host="smtp1.airws.org" userName="" password="" />   </smtp> </mailSettings> </system.net> 

****UPDATE****

I tested if an email could be sent with the usual method and an email was able to be sent using the following code.

MailMessage m = new MailMessage(new MailAddress(ConfigurationManager.AppSettings["SupportEmailAddr"]), new MailAddress(model.Email)); m.Subject = "MTSS-B: Forgot Password";  m.Body = string.Format("Here is your new password. Please go back to the MTSS-B web tool and sign in. You will be prompted to create your own password.<br/><br/>Password: " + tmpPass + "<br/><br/>MTSS-B Administrator");  m.IsBodyHtml = true; SmtpClient smtp = new SmtpClient("smtp2.airws.org");  smtp.Send(m); 
like image 814
Sun Sun Ku Avatar asked Jul 20 '15 18:07

Sun Sun Ku


1 Answers

In your app you probably have a file called IdentityConfig.cs in the App_Start folder. That file probably has something like this towards the top:

public class EmailService : IIdentityMessageService {     public Task SendAsync(IdentityMessage message)     {         // Plug in your email service here to send an email.         return Task.FromResult(0);     } } 

change it to:

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

Customizing the send code to your liking.

like image 147
Joe Avatar answered Sep 21 '22 09:09

Joe