如何为 ASP.NET 身份 UserManager.SendEmailAsync 配置发件人电子邮件凭据?

How to configure sender email credentials for ASP.NET Identity UserManager.SendEmailAsync?

我正在开发一个 Asp.net 网络应用程序。在我的应用程序中,我正在设置用户电子邮件确认和密码重置功能。我正在使用 Asp.net 内置身份系统。根据 Visual Studio 中的提及,可以在 link - https://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity 之后启用这些功能。

但要顺其自然,这个link就坏了——https://azure.microsoft.com/en-us/gallery/store/sendgrid/sendgrid-azure/。但没关系,我只想知道 asp.net 身份系统中的一件事。那就是发送电子邮件。根据 Visual Studio 中的注释行,我可以像下面这样发送重置密码电子邮件。

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

该行简单易读。但问题是我在哪里可以配置发件人电子邮件凭据?它使用什么设置来发送电子邮件?请问如何更改发件人邮箱?我也无法遵循 link,因为 Azure link 已损坏。我在哪里可以设置和更改这些设置?

我尝试在 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>

但是现在正在发送电子邮件。

我终于找到了解决办法。

我在 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>

然后我更新了

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

            return Task.FromResult(0);
        }
    }

在IdentityConfig.cs文件夹里App_Start到这个

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

        }
    }

当我发送电子邮件时,它会自动使用 web.config 中的设置。

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>