Merge pull request #5636 from sbwalker/dev
fix issues with NotificationJob related to MailKit behavior
This commit is contained in:
@ -143,65 +143,69 @@ 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;
|
||||||
log += $"NotificationId: {notification.NotificationId} - Has Missing Or Invalid Recipient {notification.ToEmail}<br />";
|
var error = "";
|
||||||
notification.IsDeleted = true;
|
|
||||||
notificationRepository.UpdateNotification(notification);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MimeMessage mailMessage = new MimeMessage();
|
|
||||||
|
|
||||||
// sender
|
// sender
|
||||||
if (settingRepository.GetSettingValue(settings, "SMTPRelay", "False") == "True" && !string.IsNullOrEmpty(notification.FromEmail))
|
if (settingRepository.GetSettingValue(settings, "SMTPRelay", "False") != "True")
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(notification.FromDisplayName))
|
fromEmail = settingRepository.GetSettingValue(settings, "SMTPSender", "");
|
||||||
{
|
fromName = !string.IsNullOrEmpty(fromName) ? fromName : site.Name;
|
||||||
mailMessage.From.Add(new MailboxAddress(notification.FromDisplayName, notification.FromEmail));
|
|
||||||
}
|
}
|
||||||
else
|
try
|
||||||
{
|
{
|
||||||
mailMessage.From.Add(new MailboxAddress("", notification.FromEmail));
|
from = new MailboxAddress(fromName, fromEmail);
|
||||||
}
|
}
|
||||||
}
|
catch
|
||||||
else
|
|
||||||
{
|
{
|
||||||
mailMessage.From.Add(new MailboxAddress((!string.IsNullOrEmpty(notification.FromDisplayName)) ? notification.FromDisplayName : site.Name,
|
// parse error creating sender mailbox address
|
||||||
settingRepository.GetSettingValue(settings, "SMTPSender", "")));
|
error += $" Invalid Sender: {fromName} <{fromEmail}>";
|
||||||
}
|
}
|
||||||
|
|
||||||
// recipient
|
// recipient
|
||||||
if (!string.IsNullOrEmpty(notification.ToDisplayName))
|
try
|
||||||
{
|
{
|
||||||
mailMessage.To.Add(new MailboxAddress(notification.ToDisplayName, notification.ToEmail));
|
to = new MailboxAddress(toName, toEmail);
|
||||||
}
|
}
|
||||||
else
|
catch
|
||||||
{
|
{
|
||||||
mailMessage.To.Add(new MailboxAddress("", notification.ToEmail));
|
// parse error creating recipient mailbox address
|
||||||
|
error += $" Invalid Recipient: {toName} <{toEmail}>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (from != null && to != null)
|
||||||
|
{
|
||||||
|
MimeMessage mailMessage = new MimeMessage();
|
||||||
|
mailMessage.From.Add(from);
|
||||||
|
mailMessage.To.Add(to);
|
||||||
|
|
||||||
// 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
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user