我正在开发一个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>
但现在发送电子邮件。
发布于 2016-11-18 19:45:28
最后我找到了解决方案。
我在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中的设置。
发布于 2019-06-27 03:13:52
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>
https://stackoverflow.com/questions/40674457
复制相似问题