首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何为ASP.NET Identity UserManager.SendEmailAsync配置发件人电子邮件凭据?

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

Stack Overflow用户
提问于 2016-11-18 18:17:01
回答 2查看 15.2K关注 0票数 4

我正在开发一个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中的注释行,我可以发送如下所示的重置密码电子邮件。

代码语言:javascript
复制
await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");

这行代码简单易读。但问题是我可以在哪里配置发件人电子邮件凭据?它使用什么设置来发送电子邮件?请问如何更改发件人的电子邮件地址?我也无法跟踪该链接,因为Azure链接已断开。我可以在哪里设置和更改这些设置?

我尝试在web.config中添加此设置

代码语言:javascript
复制
<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>

但现在发送电子邮件。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-18 19:45:28

最后我找到了解决方案。

我在web.config中添加了如下的电子邮件设置

代码语言:javascript
复制
<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>

然后我更新了

代码语言:javascript
复制
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中设置为

代码语言:javascript
复制
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中的设置。

票数 5
EN

Stack Overflow用户

发布于 2019-06-27 03:13:52

APP_Start/IdentityConfig.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40674457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档