0

I am developing an application in ASP.Net MVC, one of the requirements is reset password functionality, i cant figure out how to send password recovery email so a user can reset password.

Here is my code in the web config:

  <system.net>
  <mailSettings>
  <smtp from="from@gmail.com">
  <network host="smtp.gmail.com" defaultCredentials="false" password="password" port="587" enableSsl="true" />
  </smtp>
  </mailSettings>
  </system.net>

Here is my EmailService class in the IdentityConfig.cs:

public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // Plug in your email service here to send an email.
        SmtpClient client = new SmtpClient();
        return client.SendMailAsync("email from web config",
                                    message.Destination,
                                    message.Subject,
                                    message.Body);

     }
  }

This doesn't send email, i don't know what might the problem, hope someone can help.

Haro
  • 59
  • 1
  • 10

1 Answers1

0

I assumed you're attempting to do somewhat like this post, try using async and await keywords (since it's a System.Threading.Tasks.Task method) and wrap SmtpClient inside using statement to manage IDisposable instance easier (see this reference if you encounter so-called TaskCanceledException):

public class EmailService : IIdentityMessageService
{
    public async Task SendAsync(IdentityMessage message)
    {
        // Plug in your email service here to send an email.
        using (SmtpClient client = new SmtpClient())
        {
            await client.SendMailAsync("email from web config",
                                        message.Destination,
                                        message.Subject,
                                        message.Body);
        }
    }
}

If you don't want to use using statement, SmtpClient.SendCompleted event must be handled to dispose SmtpClient instance manually after sending mail:

public Task SendAsync(IdentityMessage message)
{
    // Plug in your email service here to send an email.
    SmtpClient client = new SmtpClient();

    // Registering SendCompleted event and dispose SmtpClient after sending message
    client.SendCompleted += (s, e) => {
        client.Dispose();
    };

    return client.SendMailAsync("email from web config",
                                message.Destination,
                                message.Subject,
                                message.Body);
}
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • Thanks for reply, i have implemented both your answer, but still no message is sent to email for password reset. I don't know what i am doing wrong. – Haro Dec 14 '17 at 07:37
  • Looking from the solution source it should be worked, have you getting exception or other errors before/after sending mail? Check if your mail account used for sending has bounced test mail. – Tetsuya Yamamoto Dec 14 '17 at 07:46