roadie/Roadie.Api.Services/EmailSenderService.cs

39 lines
1.4 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.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)
{
2019-06-30 22:14:36 +00:00
Configuration = configuration;
2019-01-08 15:51:26 +00:00
}
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
{
2019-06-30 22:14:36 +00:00
using (var mail = new MailMessage(Configuration.SmtpFromAddress, email))
2019-01-08 15:51:26 +00:00
{
2019-06-30 22:14:36 +00:00
using (var client = new SmtpClient())
2019-01-08 15:51:26 +00:00
{
2019-06-30 22:14:36 +00:00
client.Port = Configuration.SmtpPort;
client.EnableSsl = Configuration.SmtpUseSSl;
2019-01-08 15:51:26 +00:00
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
2019-06-30 22:14:36 +00:00
client.Credentials = new NetworkCredential(Configuration.SmtpUsername, Configuration.SmtpPassword);
client.Host = Configuration.SmtpHost;
2019-01-08 15:51:26 +00:00
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = htmlMessage;
2019-06-30 22:14:36 +00:00
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
2019-01-08 15:51:26 +00:00
await client.SendMailAsync(mail);
}
}
}
}
2019-01-08 22:40:26 +00:00
}