2016-11-18 91 views
1

我正在开发一个Asp.net Web应用程序。在我的应用程序中,我设置了用户电子邮件确认和密码重置功能。我正在使用身份系统内置的Asp.net。这些功能可以在此链接之后启用 - 根据在Visual Studio中提到的https://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity如何为ASP.NET Identity UserManager.SendEmailAsync配置发件人电子邮件凭据?

但是要跟着它,这个链接被打破 - 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="[email protected]"> 
     <network host="smtp.gmail.com" password="testing" port="587" userName="testing" enableSsl="true"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 

添加此设置,但是现在发送电子邮件。

+0

这是您的详细信息回答https://stackoverflow.com/a/45789677/3835843 – Arif

回答

1

最后我找到了解决方案。

我在web.config中添加有电子邮件设置这样

<system.net> 
    <mailSettings> 
     <smtp from="[email protected]"> 
     <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中的设置。

+1

您不是异步使用该异步方法。 – Sinjai

相关问题