From 05b37080c1ae6ed321321124453288c42eeec451 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Fri, 19 Sep 2025 12:45:55 -0400 Subject: [PATCH] improve NotificationJob validation logic --- .../Infrastructure/Jobs/NotificationJob.cs | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs b/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs index 85e16ff9..c85663e8 100644 --- a/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs +++ b/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs @@ -171,7 +171,7 @@ namespace Oqtane.Infrastructure // create mailbox addresses MailboxAddress to = null; MailboxAddress from = null; - var error = ""; + var mailboxAddressValidationError = ""; // sender if (settingRepository.GetSettingValue(settings, "SMTPRelay", "False") != "True") @@ -181,25 +181,41 @@ namespace Oqtane.Infrastructure } try { - from = new MailboxAddress(fromName, fromEmail); + // exception handler is necessary because of https://github.com/jstedfast/MimeKit/issues/1186 + if (MailboxAddress.TryParse(fromEmail, out _)) + { + from = new MailboxAddress(fromName, fromEmail); + } } catch { // parse error creating sender mailbox address - error += $" Invalid Sender: {fromName} <{fromEmail}>"; + } + if (from == null) + { + + mailboxAddressValidationError += $" Invalid Sender: {fromName} <{fromEmail}>"; } // recipient try { - to = new MailboxAddress(toName, toEmail); + // exception handler is necessary because of https://github.com/jstedfast/MimeKit/issues/1186 + if (MailboxAddress.TryParse(toEmail, out _)) + { + to = new MailboxAddress(toName, toEmail); + } } catch { // parse error creating recipient mailbox address - error += $" Invalid Recipient: {toName} <{toEmail}>"; + } + if (to == null) + { + mailboxAddressValidationError += $" Invalid Recipient: {toName} <{toEmail}>"; } + // if mailbox addresses are valid if (from != null && to != null) { // create mail message @@ -210,7 +226,7 @@ namespace Oqtane.Infrastructure // subject mailMessage.Subject = notification.Subject; - //body + // body var bodyText = notification.Body; if (!bodyText.Contains('<') || !bodyText.Contains('>')) @@ -235,17 +251,18 @@ namespace Oqtane.Infrastructure } catch (Exception ex) { - // error log += $"Error Sending Notification Id: {notification.NotificationId} - {ex.Message}
"; } } else { - log += $"Notification Id: {notification.NotificationId} Has An {error} And Has Been Deleted
"; + // invalid mailbox address + log += $"Notification Id: {notification.NotificationId} Has An {mailboxAddressValidationError} And Has Been Deleted
"; notification.IsDeleted = true; notificationRepository.UpdateNotification(notification); } } + log += "Notifications Delivered: " + sent + "
"; }