4

I am developing an Asp.net web application. In my application, I am setting up user email confirm and password reset feature. i am using Asp.net built in Identity system. Those features can be enabled following this link - https://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity according to mention in Visual Studio.

But to follow it, this link is broken - https://azure.microsoft.com/en-us/gallery/store/sendgrid/sendgrid-azure/. But it is ok, I want to know only one thing in asp.net identity system. That is sending email. According to the commented lines in Visual Studio, I can send reset password email like this below.

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

That line is simple and readable. But the problem is where can I configure sender email credentials? What settings it uses to send email? How can I change the sender email please? I cannot follow the link as well because Azure link is broken. Where can I set and change those settings?

I tried add this settings in web.config

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

But now sending email.

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

2 Answers2

5

Finally I found the solution.

I added there email settings in web.config like this

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

Then I updated

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

            return Task.FromResult(0);
        }
    }

in the IdentityConfig.cs in the App_Start folder to this

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 here",
                                        message.Destination,
                                        message.Subject,
                                        message.Body);

        }
    }

When I send email, it auto use the settings from the web.config.

Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • 3
    You're not using that asynchronous method asynchronously. – Sinjai Aug 11 '17 at 18:13
  • @Sinjai the method isn't marked as async, and uses no async code so it is 100% correct (and preferable) to simply return the task from the return call. – joshcomley Sep 27 '20 at 17:36
  • That's it, it's quite simple really, one thing to note is that you don't need to add the `from` field in the `` tag. – anastaciu Jan 07 '21 at 16:57
1

APP_Start/IdentityConfig.cs

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

        var smtp = new SmtpClient();
        var mail = new MailMessage();
        var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
        string username = smtpSection.Network.UserName;

        mail.IsBodyHtml = true;
        mail.From = new MailAddress(username);
        mail.To.Add(message.Destination);
        mail.Subject = message.Subject;
        mail.Body = message.Body;

        smtp.Timeout = 1000;

        await smtp.SendAsync(mail, null);
    }
}

web.config

<system.net>
<mailSettings>
  <smtp deliveryMethod="Network" from="xxxxxx@mail.com">
    <network host="smtp.xxxxx.com" userName="xxxxxx@mail.com" password="******" port="25" enableSsl="true" />
  </smtp>
</mailSettings>

</system.net>

pmarflee
  • 3,428
  • 20
  • 21