This commit is contained in:
@@ -18,7 +18,8 @@
|
|||||||
<PackageReference Include="Bogus" Version="29.0.2" />
|
<PackageReference Include="Bogus" Version="29.0.2" />
|
||||||
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.6" />
|
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.6" />
|
||||||
<PackageReference Include="Enums.NET" Version="3.0.3" />
|
<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.Authentication.JwtBearer" Version="3.1.4" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
|
||||||
|
|||||||
@@ -170,6 +170,9 @@ namespace AyaNova.Biz
|
|||||||
public static async Task<string> TestSMTPDelivery(string toAddress)
|
public static async Task<string> TestSMTPDelivery(string toAddress)
|
||||||
{
|
{
|
||||||
//DO TEST DELIVERY HERE USING EXACT SAME SETTINGS AS FOR DeliverSMTP above
|
//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";
|
return "ok";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
56
server/AyaNova/util/Mailer.cs
Normal file
56
server/AyaNova/util/Mailer.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user