roadie/Roadie.Api.Services/EmailSenderService.cs

44 lines
1.7 KiB
C#
Raw Normal View History

2019-01-08 22:40:26 +00:00
using Microsoft.AspNetCore.Identity.UI.Services;
2019-01-08 15:51:26 +00:00
using Roadie.Library.Configuration;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace Roadie.Api.Services
{
public class EmailSenderService : IEmailSender
{
2019-01-08 22:40:26 +00:00
protected IRoadieSettings Configuration { get; }
2019-01-08 15:51:26 +00:00
public EmailSenderService(IRoadieSettings configuration)
{
this.Configuration = configuration;
}
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
{
using (MailMessage mail = new MailMessage(this.Configuration.SmtpFromAddress, email))
{
using (SmtpClient client = new SmtpClient())
{
client.Port = this.Configuration.SmtpPort;
client.EnableSsl = this.Configuration.SmtpUseSSl;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(this.Configuration.SmtpUsername, this.Configuration.SmtpPassword);
client.Host = this.Configuration.SmtpHost;
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = htmlMessage;
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
await client.SendMailAsync(mail);
}
}
}
}
2019-01-08 22:40:26 +00:00
}