// https://medium.com/@kevinrodrguez/enabling-email-verification-in-asp-net-core-identity-ui-2-1-b87f028a97e0
// To fix this, we will create a new Services folder in our project and inside of it, our EmailSender.cs service:
using Microsoft.AspNetCore.Identity.UI.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MailSenderApp.Services {
public class EmailSender : IEmailSender {
public Task SendEmailAsync(string email, string subject, string htmlMessage) {
throw new NotImplementedException();
}
}
}
// ... Usings
using Microsoft.AspNetCore.Identity.UI.Services;
using MailSenderApp.Services;
namespace MailSenderApp {
public class Startup {
// ... Startup initializer and other methods
public void ConfigureServices(IServiceCollection services)
{
// ... Other services
services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
}
}
// Then, we are going to register our new service into the Dependency Injection bag by adding the following line of code to the
// Startup.cs class of our project:
// services.AddTransient<IEmailSender, EmailSender>()
using Microsoft.AspNetCore.Identity.UI.Services;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace MailSenderApp.Services {
public class EmailSender : IEmailSender {
// Our private configuration variables
private string host;
private int port;
private bool enableSSL;
private string userName;
private string password;
// Get our parameterized configuration
public EmailSender(string host, int port, bool enableSSL, string userName, string password) {
this.host = host;
this.port = port;
this.enableSSL = enableSSL;
this.userName = userName;
this.password = password;
}
// Use our configuration to send the email by using SmtpClient
public Task SendEmailAsync(string email, string subject, string htmlMessage) {
var client = new SmtpClient(host, port) {
Credentials = new NetworkCredential(userName, password),
EnableSsl = enableSSL
};
return client.SendMailAsync(
new MailMessage(userName, email, subject, htmlMessage) { IsBodyHtml = true }
);
}
}
}
// appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=myHost\\SQLINSTANCE;Database=myDataBase;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"EmailSender": {
"Host": "smtp.yourprovider.com",
"Port": 587,
"EnableSSL": true,
"UserName": "[email protected]",
"Password": "Y0urP4ssw0rd!!!"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
// And finally, we need to modify our Startup.cs to use our configuration from the appsettings.json file and passing it as parameters
// to our EmailSender service:
using Microsoft.AspNetCore.Identity.UI.Services;
using MailSenderApp.Services;
namespace MailSenderApp {
public class Startup {
// ... Startup initializer and other methods
public void ConfigureServices(IServiceCollection services)
{
// ... Other services
services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
services.AddTransient<IEmailSender, EmailSender>(i =>
new EmailSender(
Configuration["EmailSender:Host"],
Configuration.GetValue<int>("EmailSender:Port"),
Configuration.GetValue<bool>("EmailSender:EnableSSL"),
Configuration["EmailSender:UserName"],
Configuration["EmailSender:Password"]
)
);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
}
}
Comments