【问题标题】:How to configure sender email credentials for ASP.NET Identity UserManager.SendEmailAsync?如何为 ASP.NET Identity UserManager.SendEmailAsync 配置发件人电子邮件凭据?
【发布时间】:2017-04-02 03:35:39
【问题描述】:

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

但要关注它,此链接已损坏 - 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>");

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

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

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

【问题讨论】:

标签: asp.net-mvc email asp.net-identity email-client


【解决方案1】:

我终于找到了解决办法。

我像这样在 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);
        }
    }

在App_Start文件夹中的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 here",
                                        message.Destination,
                                        message.Subject,
                                        message.Body);

        }
    }

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

【讨论】:

  • 你没有异步使用那个异步方法。
  • @Sinjai 该方法未标记为异步,并且不使用异步代码,因此从返回调用中简单地返回任务是 100% 正确(并且更可取)。
  • 就是这样,真的很简单,需要注意的一点是你不需要在&lt;smtp&gt;标签中添加from字段。
【解决方案2】:

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>

【讨论】:

    猜你喜欢
    • 2018-02-13
    • 2016-03-10
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多