Merge pull request #5636 from sbwalker/dev

fix issues with NotificationJob related to MailKit behavior
This commit is contained in:
Shaun Walker
2025-09-18 17:24:05 -04:00
committed by GitHub
2 changed files with 55 additions and 45 deletions

View File

@ -143,64 +143,68 @@ namespace Oqtane.Infrastructure
List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList(); List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList();
foreach (Notification notification in notifications) foreach (Notification notification in notifications)
{ {
// get sender and receiver information from user object if not provided var fromEmail = notification.FromEmail ?? "";
if ((string.IsNullOrEmpty(notification.FromEmail) || string.IsNullOrEmpty(notification.FromDisplayName)) && notification.FromUserId != null) var fromName = notification.FromDisplayName ?? "";
var toEmail = notification.ToEmail ?? "";
var toName = notification.ToDisplayName ?? "";
// get sender and receiver information from user information if available
if ((string.IsNullOrEmpty(fromEmail) || string.IsNullOrEmpty(fromName)) && notification.FromUserId != null)
{ {
var user = userRepository.GetUser(notification.FromUserId.Value); var user = userRepository.GetUser(notification.FromUserId.Value);
if (user != null) if (user != null)
{ {
notification.FromEmail = (string.IsNullOrEmpty(notification.FromEmail)) ? user.Email : notification.FromEmail; fromEmail = string.IsNullOrEmpty(fromEmail) ? user.Email ?? "" : fromEmail;
notification.FromDisplayName = (string.IsNullOrEmpty(notification.FromDisplayName)) ? user.DisplayName : notification.FromDisplayName; fromName = string.IsNullOrEmpty(fromName) ? user.DisplayName ?? "" : fromName;
} }
} }
if ((string.IsNullOrEmpty(notification.ToEmail) || string.IsNullOrEmpty(notification.ToDisplayName)) && notification.ToUserId != null) if ((string.IsNullOrEmpty(toEmail) || string.IsNullOrEmpty(toName)) && notification.ToUserId != null)
{ {
var user = userRepository.GetUser(notification.ToUserId.Value); var user = userRepository.GetUser(notification.ToUserId.Value);
if (user != null) if (user != null)
{ {
notification.ToEmail = (string.IsNullOrEmpty(notification.ToEmail)) ? user.Email : notification.ToEmail; toEmail = string.IsNullOrEmpty(toEmail) ? user.Email ?? "" : toEmail;
notification.ToDisplayName = (string.IsNullOrEmpty(notification.ToDisplayName)) ? user.DisplayName : notification.ToDisplayName; toName = string.IsNullOrEmpty(toName) ? user.DisplayName ?? "" : toName;
} }
} }
// validate recipient // create mailbox addresses
if (string.IsNullOrEmpty(notification.ToEmail) || !MailboxAddress.TryParse(notification.ToEmail, out _)) MailboxAddress to = null;
MailboxAddress from = null;
var error = "";
// sender
if (settingRepository.GetSettingValue(settings, "SMTPRelay", "False") != "True")
{ {
log += $"NotificationId: {notification.NotificationId} - Has Missing Or Invalid Recipient {notification.ToEmail}<br />"; fromEmail = settingRepository.GetSettingValue(settings, "SMTPSender", "");
notification.IsDeleted = true; fromName = !string.IsNullOrEmpty(fromName) ? fromName : site.Name;
notificationRepository.UpdateNotification(notification);
} }
else try
{
from = new MailboxAddress(fromName, fromEmail);
}
catch
{
// parse error creating sender mailbox address
error += $" Invalid Sender: {fromName} &lt;{fromEmail}&gt;";
}
// recipient
try
{
to = new MailboxAddress(toName, toEmail);
}
catch
{
// parse error creating recipient mailbox address
error += $" Invalid Recipient: {toName} &lt;{toEmail}&gt;";
}
if (from != null && to != null)
{ {
MimeMessage mailMessage = new MimeMessage(); MimeMessage mailMessage = new MimeMessage();
mailMessage.From.Add(from);
// sender mailMessage.To.Add(to);
if (settingRepository.GetSettingValue(settings, "SMTPRelay", "False") == "True" && !string.IsNullOrEmpty(notification.FromEmail))
{
if (!string.IsNullOrEmpty(notification.FromDisplayName))
{
mailMessage.From.Add(new MailboxAddress(notification.FromDisplayName, notification.FromEmail));
}
else
{
mailMessage.From.Add(new MailboxAddress("", notification.FromEmail));
}
}
else
{
mailMessage.From.Add(new MailboxAddress((!string.IsNullOrEmpty(notification.FromDisplayName)) ? notification.FromDisplayName : site.Name,
settingRepository.GetSettingValue(settings, "SMTPSender", "")));
}
// recipient
if (!string.IsNullOrEmpty(notification.ToDisplayName))
{
mailMessage.To.Add(new MailboxAddress(notification.ToDisplayName, notification.ToEmail));
}
else
{
mailMessage.To.Add(new MailboxAddress("", notification.ToEmail));
}
// subject // subject
mailMessage.Subject = notification.Subject; mailMessage.Subject = notification.Subject;
@ -231,13 +235,19 @@ namespace Oqtane.Infrastructure
catch (Exception ex) catch (Exception ex)
{ {
// error // error
log += $"NotificationId: {notification.NotificationId} - {ex.Message}<br />"; log += $"Error Sending Notification Id: {notification.NotificationId} - {ex.Message}<br />";
} }
} }
else
{
log += $"Notification Id: {notification.NotificationId} - Has An {error} And Has Been Deleted<br />";
notification.IsDeleted = true;
notificationRepository.UpdateNotification(notification);
}
} }
await client.DisconnectAsync(true);
log += "Notifications Delivered: " + sent + "<br />"; log += "Notifications Delivered: " + sent + "<br />";
} }
await client.DisconnectAsync(true);
} }
} }
else else

View File

@ -144,14 +144,14 @@ namespace Oqtane.Repository
// delete notifications in batches of 100 records // delete notifications in batches of 100 records
var count = 0; var count = 0;
var purgedate = DateTime.UtcNow.AddDays(-age); var purgedate = DateTime.UtcNow.AddDays(-age);
var notifications = db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && item.IsDelivered && item.DeliveredOn < purgedate) var notifications = db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && (item.IsDeleted || item.IsDelivered && item.DeliveredOn < purgedate))
.OrderBy(item => item.DeliveredOn).Take(100).ToList(); .OrderBy(item => item.DeliveredOn).Take(100).ToList();
while (notifications.Count > 0) while (notifications.Count > 0)
{ {
count += notifications.Count; count += notifications.Count;
db.Notification.RemoveRange(notifications); db.Notification.RemoveRange(notifications);
db.SaveChanges(); db.SaveChanges();
notifications = db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && item.IsDelivered && item.DeliveredOn < purgedate) notifications = db.Notification.Where(item => item.SiteId == siteId && item.FromUserId == null && (item.IsDeleted || item.IsDelivered && item.DeliveredOn < purgedate))
.OrderBy(item => item.DeliveredOn).Take(100).ToList(); .OrderBy(item => item.DeliveredOn).Take(100).ToList();
} }
return count; return count;