This commit is contained in:
2020-07-22 00:12:03 +00:00
parent 662af797e1
commit f9f55c95d7
3 changed files with 61 additions and 1 deletions

View File

@@ -18,7 +18,8 @@
<PackageReference Include="Bogus" Version="29.0.2" />
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.6" />
<PackageReference Include="Enums.NET" Version="3.0.3" />
<PackageReference Include="jose-jwt" Version="2.5.0" />
<PackageReference Include="jose-jwt" Version="2.5.0" />
<PackageReference Include="MailKit" Version="2.8.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />

View File

@@ -170,6 +170,9 @@ namespace AyaNova.Biz
public static async Task<string> 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";
}

View File

@@ -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);
}
}
}
}