diff --git a/server/AyaNova/Startup.cs b/server/AyaNova/Startup.cs index 451f9d86..8cd3dab9 100644 --- a/server/AyaNova/Startup.cs +++ b/server/AyaNova/Startup.cs @@ -75,6 +75,10 @@ namespace AyaNova _newLog.LogDebug("ServerState service"); services.AddSingleton(new AyaNova.Api.ControllerHelpers.ApiServerState()); + + _newLog.LogDebug("Mail service"); + services.AddSingleton(); + //Init controllers _newLog.LogDebug("Controllers"); var MvcBuilder = services.AddControllers(config => diff --git a/server/AyaNova/generator/CoreJobNotify.cs b/server/AyaNova/generator/CoreJobNotify.cs index 6878f09d..63bf9c44 100644 --- a/server/AyaNova/generator/CoreJobNotify.cs +++ b/server/AyaNova/generator/CoreJobNotify.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using AyaNova.Models; +using AyaNova.Util; namespace AyaNova.Biz { @@ -16,6 +17,9 @@ namespace AyaNova.Biz { private static bool NotifyIsRunning = false; private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("CoreJobNotify"); + + + private static DateTime lastRun = DateTime.MinValue; // private static TimeSpan DELETE_AFTER_AGE = new TimeSpan(90, 0, 0, 0); // private static TimeSpan RUN_EVERY_INTERVAL = new TimeSpan(0, 2, 0);//once every 2 minutes minimum @@ -157,7 +161,7 @@ namespace AyaNova.Biz private static async Task DeliverSMTP(NotifyEvent ne, AyContext ct) { log.LogTrace($"DeliverSMTP deliving notify event: {ne}"); - throw new System.NotImplementedException("DeliverSMTP: NOT IMPLEMENTED YET"); + //todo: //Open question: what to do with failed deliveries? //we dont' want them piling up but we don't want to just dump them do we? //it should be only mail ones that fail, not app ones, there's no way for an app delivery to fail as it's just put in a table @@ -173,7 +177,17 @@ namespace AyaNova.Biz //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"; + + IMailer m = AyaNova.Util.ServiceProviderProvider.Mailer; + try + { + await m.SendEmailAsync(toAddress, "Test from Notification system", "This is a test to confirm notification system is working", ServerGlobalOpsSettingsCache.Notify); + return "ok"; + } + catch (Exception ex) + { + return ExceptionUtil.ExtractAllExceptionMessages(ex); + } } diff --git a/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs b/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs index 00566f0e..4466bd8f 100644 --- a/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs +++ b/server/AyaNova/util/ServerGlobalOpsSettingsCache.cs @@ -16,7 +16,7 @@ namespace AyaNova.Util internal static bool BOOTING { get; set; } internal static GlobalOpsBackupSettings Backup { get; set; } - internal static GlobalOpsNotificationSettings Notify {get;set;} + internal static GlobalOpsNotificationSettings Notify { get; set; } internal static DateTime NextBackup { get; set; } /// /// Populate and / or create the settings @@ -35,10 +35,12 @@ namespace AyaNova.Util SetNextBackup(); - Notify=ct.GlobalOpsNotificationSettings.FirstOrDefault(z=>z.Id==1); - if(Notify==null) + Notify = ct.GlobalOpsNotificationSettings.FirstOrDefault(z => z.Id == 1); + if (Notify == null) { - Notify=new GlobalOpsNotificationSettings(); + Notify = new GlobalOpsNotificationSettings(); + ct.GlobalOpsNotificationSettings.Add(Notify); + ct.SaveChanges(); } } diff --git a/server/AyaNova/util/ServiceProviderProvider.cs b/server/AyaNova/util/ServiceProviderProvider.cs index 9bf4d4a3..7f4e9e8c 100644 --- a/server/AyaNova/util/ServiceProviderProvider.cs +++ b/server/AyaNova/util/ServiceProviderProvider.cs @@ -40,6 +40,14 @@ namespace AyaNova.Util } } + internal static AyaNova.Util.IMailer Mailer + { + get + { + return Scope.ServiceProvider.GetRequiredService(); + } + } + // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1 internal static System.Net.Http.IHttpClientFactory HttpClientFactory {