From f412e6a66bc41403dc30c6dfe9ee499812ac7dc1 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 21 Nov 2022 00:25:14 +0000 Subject: [PATCH] case 4310 --- .../AyaNova/Controllers/NotifyController.cs | 26 +++++++++---------- server/AyaNova/util/Mailer.cs | 20 ++++++++++---- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/server/AyaNova/Controllers/NotifyController.cs b/server/AyaNova/Controllers/NotifyController.cs index 9f22ea7c..0c7f8b07 100644 --- a/server/AyaNova/Controllers/NotifyController.cs +++ b/server/AyaNova/Controllers/NotifyController.cs @@ -243,8 +243,18 @@ namespace AyaNova.Api.Controllers if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); + if (!ServerGlobalOpsSettingsCache.Notify.SmtpDeliveryActive) + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, null, "Email notifications are set to OFF at server, unable to send 'on request' type SMTP notification")); - //validate + //for now I'm allowing any authenticated user to call this route intentionally as the use case is for our own internal messaging from the Customer form only or for API users who want to send + //email when rendering a report (case 4310). + + //validate incoming data + if (string.IsNullOrWhiteSpace(notifyDirectSMTP.Subject)) + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, "Subject", "Subject field is required")); + + if (string.IsNullOrWhiteSpace(notifyDirectSMTP.TextBody) && string.IsNullOrWhiteSpace(notifyDirectSMTP.HTMLBody)) + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, null, "TextBody or HTMLBody field is required")); if (string.IsNullOrWhiteSpace(notifyDirectSMTP.ToAddress)) { @@ -277,19 +287,7 @@ namespace AyaNova.Api.Controllers IMailer m = AyaNova.Util.ServiceProviderProvider.Mailer; try { - if (!ServerGlobalOpsSettingsCache.Notify.SmtpDeliveryActive) - { - await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.GeneralNotification, - $"Email notifications are set to OFF at server, unable to send 'on request' type SMTP notification subject:{notifyDirectSMTP.Subject}", - "Error", - null, - UserId); - log.LogInformation($"** WARNING: SMTP notification is currently set to Active=False; unable to send 'on request' type SMTP notification subject:{notifyDirectSMTP.Subject} **"); - - } - else - await m.SendEmailAsync(notifyDirectSMTP.ToAddress, "Test from Notification system", "This is a test to confirm notification system is working", ServerGlobalOpsSettingsCache.Notify); - + await m.SendEmailAsync(notifyDirectSMTP.ToAddress, "Test from Notification system", "This is a test to confirm notification system is working", ServerGlobalOpsSettingsCache.Notify); } catch (Exception ex) { diff --git a/server/AyaNova/util/Mailer.cs b/server/AyaNova/util/Mailer.cs index 1800846b..2fd8fea2 100644 --- a/server/AyaNova/util/Mailer.cs +++ b/server/AyaNova/util/Mailer.cs @@ -9,7 +9,7 @@ namespace AyaNova.Util { public interface IMailer { - Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string attachPDF = null, string forceFileName = null); + Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string attachPDF = null, string forceFileName = null, string htmlBody = null); } public class Mailer : IMailer @@ -19,7 +19,7 @@ namespace AyaNova.Util } - public async Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string attachPDFPath = null, string forceFileName = null) + public async Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string attachPDFPath = null, string forceFileName = null, string htmlBody = null) { try { @@ -49,8 +49,18 @@ namespace AyaNova.Util } else { + // if (!string.IsNullOrWhiteSpace(body)) + // message.Body = new TextPart(TextFormat.Plain) { Text = body }; + + MimeKit.BodyBuilder builder = new BodyBuilder(); + if (!string.IsNullOrWhiteSpace(body)) - message.Body = new TextPart(TextFormat.Plain) { Text = body }; + builder.TextBody = body; + + if (!string.IsNullOrWhiteSpace(htmlBody)) + builder.HtmlBody = htmlBody; + + message.Body = builder.ToMessageBody(); } using (var client = new SmtpClient()) @@ -67,7 +77,7 @@ namespace AyaNova.Util DisposeStreamsInMimeMessage(message); } - + } catch (Exception e) { @@ -75,7 +85,7 @@ namespace AyaNova.Util } } - public static void DisposeStreamsInMimeMessage(MimeMessage msg) { foreach (var part in msg.BodyParts) (part as MimePart)?.Content?.Stream?.Dispose(); } + public static void DisposeStreamsInMimeMessage(MimeMessage msg) { foreach (var part in msg.BodyParts) (part as MimePart)?.Content?.Stream?.Dispose(); } } } \ No newline at end of file