hunostor

Identity Email Sender

Feb 19th, 2019
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 KB | None | 0 0
  1. // https://medium.com/@kevinrodrguez/enabling-email-verification-in-asp-net-core-identity-ui-2-1-b87f028a97e0
  2.  
  3. // To fix this, we will create a new Services folder in our project and inside of it, our EmailSender.cs service:
  4. using Microsoft.AspNetCore.Identity.UI.Services;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9.  
  10. namespace MailSenderApp.Services {
  11.     public class EmailSender : IEmailSender {
  12.         public Task SendEmailAsync(string email, string subject, string htmlMessage) {
  13.             throw new NotImplementedException();
  14.         }
  15.     }
  16. }
  17.  
  18. // ... Usings
  19. using Microsoft.AspNetCore.Identity.UI.Services;
  20. using MailSenderApp.Services;
  21.  
  22. namespace MailSenderApp {
  23.     public class Startup {
  24.        
  25.         // ... Startup initializer and other methods
  26.        
  27.         public void ConfigureServices(IServiceCollection services)
  28.         {
  29.             // ... Other services
  30.             services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
  31.  
  32.             services.AddTransient<IEmailSender, EmailSender>();
  33.  
  34.             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  35.         }
  36.     }
  37. }
  38.  
  39.  
  40. // Then, we are going to register our new service into the Dependency Injection bag by adding the following line of code to the
  41. // Startup.cs class of our project:
  42. // services.AddTransient<IEmailSender, EmailSender>()
  43.  
  44. using Microsoft.AspNetCore.Identity.UI.Services;
  45. using System;
  46. using System.Collections.Generic;
  47. using System.Net;
  48. using System.Net.Mail;
  49. using System.Text;
  50. using System.Threading.Tasks;
  51.  
  52. namespace MailSenderApp.Services {
  53.     public class EmailSender : IEmailSender {
  54.  
  55.         // Our private configuration variables
  56.         private string host;
  57.         private int port;
  58.         private bool enableSSL;
  59.         private string userName;
  60.         private string password;
  61.        
  62.         // Get our parameterized configuration
  63.         public EmailSender(string host, int port, bool enableSSL, string userName, string password) {
  64.             this.host = host;
  65.             this.port = port;
  66.             this.enableSSL = enableSSL;
  67.             this.userName = userName;
  68.             this.password = password;
  69.         }
  70.        
  71.         // Use our configuration to send the email by using SmtpClient
  72.         public Task SendEmailAsync(string email, string subject, string htmlMessage) {
  73.             var client = new SmtpClient(host, port) {
  74.                 Credentials = new NetworkCredential(userName, password),
  75.                 EnableSsl = enableSSL
  76.             };
  77.             return client.SendMailAsync(
  78.                 new MailMessage(userName, email, subject, htmlMessage) { IsBodyHtml = true }
  79.             );
  80.         }
  81.     }
  82. }
  83.  
  84.  
  85. // appsettings.json
  86. {
  87.   "ConnectionStrings": {
  88.     "DefaultConnection": "Server=myHost\\SQLINSTANCE;Database=myDataBase;Trusted_Connection=True;MultipleActiveResultSets=true"
  89.   },
  90.   "EmailSender": {
  91.     "Host": "smtp.yourprovider.com",
  92.     "Port": 587,
  93.     "EnableSSL": true,
  94.     "UserName": "[email protected]",
  95.     "Password":  "Y0urP4ssw0rd!!!"
  96.   },
  97.   "Logging": {
  98.     "LogLevel": {
  99.       "Default": "Warning"
  100.     }
  101.   },
  102.   "AllowedHosts": "*"
  103. }
  104.  
  105. // And finally, we need to modify our Startup.cs to use our configuration from the appsettings.json file and passing it as parameters
  106. // to our EmailSender service:
  107.  
  108. using Microsoft.AspNetCore.Identity.UI.Services;
  109. using MailSenderApp.Services;
  110.  
  111. namespace MailSenderApp {
  112.     public class Startup {
  113.        
  114.         // ... Startup initializer and other methods
  115.        
  116.         public void ConfigureServices(IServiceCollection services)
  117.         {
  118.             // ... Other services
  119.             services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
  120.  
  121.             services.AddTransient<IEmailSender, EmailSender>(i =>
  122.                 new EmailSender(
  123.                     Configuration["EmailSender:Host"],
  124.                     Configuration.GetValue<int>("EmailSender:Port"),
  125.                     Configuration.GetValue<bool>("EmailSender:EnableSSL"),
  126.                     Configuration["EmailSender:UserName"],
  127.                     Configuration["EmailSender:Password"]
  128.                 )
  129.             );
  130.  
  131.             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment