From f9f55c95d7776a1c6254cfd9135fba655ad534ac Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 22 Jul 2020 00:12:03 +0000 Subject: [PATCH] --- server/AyaNova/AyaNova.csproj | 3 +- server/AyaNova/generator/CoreJobNotify.cs | 3 ++ server/AyaNova/util/Mailer.cs | 56 +++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 server/AyaNova/util/Mailer.cs diff --git a/server/AyaNova/AyaNova.csproj b/server/AyaNova/AyaNova.csproj index 71b1b323..4441b0de 100644 --- a/server/AyaNova/AyaNova.csproj +++ b/server/AyaNova/AyaNova.csproj @@ -18,7 +18,8 @@ - + + diff --git a/server/AyaNova/generator/CoreJobNotify.cs b/server/AyaNova/generator/CoreJobNotify.cs index 402b3071..6878f09d 100644 --- a/server/AyaNova/generator/CoreJobNotify.cs +++ b/server/AyaNova/generator/CoreJobNotify.cs @@ -170,6 +170,9 @@ namespace AyaNova.Biz public static async Task TestSMTPDelivery(string toAddress) { //DO TEST DELIVERY HERE USING EXACT SAME SETTINGS AS FOR DeliverSMTP above + //todo: abstract out email sending to it's own class maybe or whatever method I choose supports the best + //https://jasonwatmore.com/post/2020/07/15/aspnet-core-3-send-emails-via-smtp-with-mailkit + //https://medium.com/@ffimnsr/sending-email-using-mailkit-in-asp-net-core-web-api-71b946380442 return "ok"; } diff --git a/server/AyaNova/util/Mailer.cs b/server/AyaNova/util/Mailer.cs new file mode 100644 index 00000000..6cffc7d4 --- /dev/null +++ b/server/AyaNova/util/Mailer.cs @@ -0,0 +1,56 @@ +using MailKit.Net.Smtp; +using MimeKit; +using System; +using System.Threading.Tasks; +using MimeKit.Text; + +namespace AyaNova.Util +{ + public interface IMailer + { + Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string bodyHTML = null); + } + + public class Mailer : IMailer + { + public Mailer() + { + + } + + public async Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string bodyHTML = null) + { + try + { + var message = new MimeMessage(); + message.From.Add(new MailboxAddress(smtpSettings.NotifyFromAddress, smtpSettings.NotifyFromAddress)); + message.To.Add(MailboxAddress.Parse(email)); + message.Subject = subject; + if (!string.IsNullOrWhiteSpace(body)) + message.Body = new TextPart(TextFormat.Plain) { Text = body }; + if (!string.IsNullOrWhiteSpace(bodyHTML)) + message.Body = new TextPart("html") + { + Text = body + }; + + using (var client = new SmtpClient()) + { + client.ServerCertificateValidationCallback = (s, c, h, e) => true; + + bool UseSSL = smtpSettings.ConnectionSecurity != Biz.NotifyMailSecurity.None; + await client.ConnectAsync(smtpSettings.SmtpServerAddress, smtpSettings.SmtpServerPort, UseSSL); + + + await client.AuthenticateAsync(smtpSettings.SmtpAccount, smtpSettings.SmtpPassword); + await client.SendAsync(message); + await client.DisconnectAsync(true); + } + } + catch (Exception e) + { + throw new InvalidOperationException(e.Message); + } + } + } +} \ No newline at end of file