[英]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
): 我假设您正在尝试执行类似此文章的尝试,尝试使用
async
和await
关键字(因为它是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.