Skip to content

Instantly share code, notes, and snippets.

@kevinrodriguez-io
Last active May 25, 2019 16:22
Show Gist options
  • Save kevinrodriguez-io/d1bc0c718fdccd83c11b3825a4fe91c3 to your computer and use it in GitHub Desktop.
Save kevinrodriguez-io/d1bc0c718fdccd83c11b3825a4fe91c3 to your computer and use it in GitHub Desktop.
ASP.Net Core Identity UI 2.1 EmailSender using SmtpClient
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 }
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment