How to configure sender email credentials for ASP.NET Identity UserManager.SendEmailAsync?
Solution 1
Finally I found the solution.
I added there email settings in web.config like this
<system.net>
<mailSettings>
<smtp from="[email protected]">
<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.
Solution 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;
await smtp.SendAsync(mail, null);
}
}
web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="[email protected]">
<network host="smtp.xxxxx.com" userName="[email protected]" password="******" port="25" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
Comments
-
Wai Yan Hein almost 2 years
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="[email protected]"> <network host="smtp.gmail.com" password="testing" port="587" userName="testing" enableSsl="true"/> </smtp> </mailSettings> </system.net>
But now sending email.
-
Sinjai over 6 yearsYou're not using that asynchronous method asynchronously.
-
joshcomley over 3 years@Sinjai the method isn't marked as async, and uses no async code so it is 100% correct (and preferable) to simply return the task from the return call.
-
anastaciu over 3 yearsThat's it, it's quite simple really, one thing to note is that you don't need to add the
from
field in the<smtp>
tag.