简体   繁体   English

如何在IdentityConfig.cs中配置EmailService类以发送电子邮件以在asp.net中重置密码

[英]How to configure EmailService class in IdentityConfig.cs to send email for password reset in asp.net

I am developing an application in ASP.Net MVC, one of the requirements is reset password functionality, i cant figure out how to send password recovery email so a user can reset password. 我正在ASP.Net MVC中开发应用程序,要求之一是重置密码功能,我无法弄清楚如何发送密码恢复电子邮件,以便用户可以重置密码。

Here is my code in the web config: 这是我在网络配置中的代码:

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

Here is my EmailService class in the IdentityConfig.cs: 这是我的IdentityConfig.cs中的EmailService类:

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

     }
  }

This doesn't send email, i don't know what might the problem, hope someone can help. 这不会发送电子邮件,我不知道可能是什么问题,希望有人可以提供帮助。

I assumed you're attempting to do somewhat like this post , try using async and await keywords (since it's a System.Threading.Tasks.Task method) and wrap SmtpClient inside using statement to manage IDisposable instance easier (see this reference if you encounter so-called TaskCanceledException ): 我假设您正在尝试执行类似此文章的尝试,尝试使用asyncawait关键字(因为它是System.Threading.Tasks.Task方法),并将SmtpClient包裹在using语句中,以便更轻松地管理IDisposable实例(如果遇到此问题,请参见此参考)所谓TaskCanceledException ):

public class EmailService : IIdentityMessageService
{
    public async Task SendAsync(IdentityMessage message)
    {
        // Plug in your email service here to send an email.
        using (SmtpClient client = new SmtpClient())
        {
            await client.SendMailAsync("email from web config",
                                        message.Destination,
                                        message.Subject,
                                        message.Body);
        }
    }
}

If you don't want to use using statement, SmtpClient.SendCompleted event must be handled to dispose SmtpClient instance manually after sending mail: 如果您不想使用using语句,则必须处理SmtpClient.SendCompleted事件,以在发送邮件后手动处置SmtpClient实例:

public Task SendAsync(IdentityMessage message)
{
    // Plug in your email service here to send an email.
    SmtpClient client = new SmtpClient();

    // Registering SendCompleted event and dispose SmtpClient after sending message
    client.SendCompleted += (s, e) => {
        client.Dispose();
    };

    return client.SendMailAsync("email from web config",
                                message.Destination,
                                message.Subject,
                                message.Body);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 现在已删除了identityconfig.cs,在ASP.NET 5.0 MVC Identity中定义密码验证的正确位置在哪里? - Where is the correct place to define password validation in ASP.NET 5.0 MVC Identity now identityconfig.cs has been removed? ASP.NET MVC 应用程序:无法在 IdentityConfig.cs 中的 ApplicationUserManager.RegisterTwoFactorProvider 中本地化 MessageFormat - ASP.NET MVC application : could not localize MessageFormat in ApplicationUserManager.RegisterTwoFactorProvider in IdentityConfig.cs 在没有密码的情况下在ASP.net中发送电子邮件 - Send email in ASP.net without password 如何从旧的asp.net成员身份创建用户到新的MVC .net并发送密码重置 - How to create users from old asp.net membership to new MVC .net and send password reset Asp.Net-身份2-在EmailService触发的电子邮件中附加文件 - Asp.Net - Identity 2 - Attach files in emails fired by EmailService 带Angularjs的MVC,ASP.NET身份,EmailService和异步调用 - MVC, ASP.NET Identity, EmailService and Async calls w/Angularjs 如何在 Asp.Net 中发送带有附件的电子邮件 - How to Send Email With Attachment In Asp.Net 发送忘记密码到电子邮件 Asp.net 代码错误 - Send forget password to email Asp.net code error ASP.NET成员资格提供程序 - 重置密码功能 - 电子邮件确认和密码更改 - ASP.NET Membership Provider - Reset Password Features - Email Confirmation and Password Change 如何在ASP.NET MVC中生成令牌以重置密码 - How to generate token to reset password in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM