问题
I am developing an Asp.net web application. In my application, I am setting up user email confirm and password reset feature. i am using Asp.net built in Identity system. Those features can be enabled following this link - https://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity according to mention in Visual Studio.
But to follow it, this link is broken - https://azure.microsoft.com/en-us/gallery/store/sendgrid/sendgrid-azure/. But it is ok, I want to know only one thing in asp.net identity system. That is sending email. According to the commented lines in Visual Studio, I can send reset password email like this below.
await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
That line is simple and readable. But the problem is where can I configure sender email credentials? What settings it uses to send email? How can I change the sender email please? I cannot follow the link as well because Azure link is broken. Where can I set and change those settings?
I tried add this settings in 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>
But now sending email.
回答1:
Finally I found the solution.
I added there email settings in web.config like this
<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>
Then I updated
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
return Task.FromResult(0);
}
}
in the IdentityConfig.cs in the App_Start folder to this
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);
}
}
When I send email, it auto use the settings from the web.config.
回答2:
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;
var t = Task.Run(() => smtp.SendAsync(mail, null));
return t;
}
}
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/how-to-configure-sender-email-credentials-for-asp-net-identity-usermanager-sende