diff --git a/Oqtane.Client/Modules/Admin/Site/Index.razor b/Oqtane.Client/Modules/Admin/Site/Index.razor index c0f2df68..904e19c7 100644 --- a/Oqtane.Client/Modules/Admin/Site/Index.razor +++ b/Oqtane.Client/Modules/Admin/Site/Index.razor @@ -145,9 +145,9 @@
- +
- @@ -183,7 +183,16 @@
-
+
+ +
+ +
+
+
@@ -354,6 +363,7 @@ private string _togglesmtppassword = string.Empty; private string _smtpsender = string.Empty; private string _smtprelay = "False"; + private string _smtpenabled = "True"; private string _retention = string.Empty; private string _pwaisenabled; private int _pwaappiconfileid = -1; @@ -434,6 +444,7 @@ _togglesmtppassword = SharedLocalizer["ShowPassword"]; _smtpsender = SettingService.GetSetting(settings, "SMTPSender", string.Empty); _smtprelay = SettingService.GetSetting(settings, "SMTPRelay", "False"); + _smtpenabled = SettingService.GetSetting(settings, "SMTPEnabled", "True"); _retention = SettingService.GetSetting(settings, "NotificationRetention", "30"); // aliases @@ -600,7 +611,8 @@ settings = SettingService.SetSetting(settings, "SMTPPassword", _smtppassword, true); settings = SettingService.SetSetting(settings, "SMTPSender", _smtpsender, true); settings = SettingService.SetSetting(settings, "SMTPRelay", _smtprelay, true); - settings = SettingService.SetSetting(settings, "NotificationRetention", _retention, true); + settings = SettingService.SetSetting(settings, "SMTPEnabled", _smtpenabled, true); + settings = SettingService.SetSetting(settings, "NotificationRetention", _retention, true); await SettingService.UpdateSiteSettingsAsync(settings, site.SiteId); await logger.LogInformation("Site Settings Saved {Site}", site); diff --git a/Oqtane.Client/Resources/Modules/Admin/Site/Index.resx b/Oqtane.Client/Resources/Modules/Admin/Site/Index.resx index 5abaf9f2..0f9d634e 100644 --- a/Oqtane.Client/Resources/Modules/Admin/Site/Index.resx +++ b/Oqtane.Client/Resources/Modules/Admin/Site/Index.resx @@ -369,4 +369,10 @@ Page Content + + Specify if SMTP is enabled for this site + + + Enabled? + \ No newline at end of file diff --git a/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs b/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs index ea12ac21..a761bd0f 100644 --- a/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs +++ b/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs @@ -42,119 +42,127 @@ namespace Oqtane.Infrastructure // get site settings List sitesettings = settingRepository.GetSettings(EntityNames.Site, site.SiteId).ToList(); Dictionary settings = GetSettings(sitesettings); - if (settings.ContainsKey("SMTPHost") && settings["SMTPHost"] != "" && - settings.ContainsKey("SMTPPort") && settings["SMTPPort"] != "" && - settings.ContainsKey("SMTPSSL") && settings["SMTPSSL"] != "" && - settings.ContainsKey("SMTPSender") && settings["SMTPSender"] != "") + if (!settings.ContainsKey("SMTPEnabled") || settings["SMTPEnabled"] == "True") { - // construct SMTP Client - var client = new SmtpClient() + if (settings.ContainsKey("SMTPHost") && settings["SMTPHost"] != "" && + settings.ContainsKey("SMTPPort") && settings["SMTPPort"] != "" && + settings.ContainsKey("SMTPSSL") && settings["SMTPSSL"] != "" && + settings.ContainsKey("SMTPSender") && settings["SMTPSender"] != "") { - DeliveryMethod = SmtpDeliveryMethod.Network, - UseDefaultCredentials = false, - Host = settings["SMTPHost"], - Port = int.Parse(settings["SMTPPort"]), - EnableSsl = bool.Parse(settings["SMTPSSL"]) - }; - if (settings["SMTPUsername"] != "" && settings["SMTPPassword"] != "") - { - client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]); - } - - // iterate through undelivered notifications - int sent = 0; - List notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList(); - foreach (Notification notification in notifications) - { - // get sender and receiver information from user object if not provided - if ((string.IsNullOrEmpty(notification.FromEmail) || string.IsNullOrEmpty(notification.FromDisplayName)) && notification.FromUserId != null) + // construct SMTP Client + var client = new SmtpClient() { - var user = userRepository.GetUser(notification.FromUserId.Value); - if (user != null) - { - notification.FromEmail = (string.IsNullOrEmpty(notification.FromEmail)) ? user.Email : notification.FromEmail; - notification.FromDisplayName = (string.IsNullOrEmpty(notification.FromDisplayName)) ? user.DisplayName : notification.FromDisplayName; - } - } - if ((string.IsNullOrEmpty(notification.ToEmail) || string.IsNullOrEmpty(notification.ToDisplayName)) && notification.ToUserId != null) + DeliveryMethod = SmtpDeliveryMethod.Network, + UseDefaultCredentials = false, + Host = settings["SMTPHost"], + Port = int.Parse(settings["SMTPPort"]), + EnableSsl = bool.Parse(settings["SMTPSSL"]) + }; + if (settings["SMTPUsername"] != "" && settings["SMTPPassword"] != "") { - var user = userRepository.GetUser(notification.ToUserId.Value); - if (user != null) - { - notification.ToEmail = (string.IsNullOrEmpty(notification.ToEmail)) ? user.Email : notification.ToEmail; - notification.ToDisplayName = (string.IsNullOrEmpty(notification.ToDisplayName)) ? user.DisplayName : notification.ToDisplayName; - } + client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]); } - // validate recipient - if (string.IsNullOrEmpty(notification.ToEmail)) + // iterate through undelivered notifications + int sent = 0; + List notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList(); + foreach (Notification notification in notifications) { - log += "Recipient Missing For NotificationId: " + notification.NotificationId + "
"; - notification.IsDeleted = true; - notificationRepository.UpdateNotification(notification); - } - else - { - MailMessage mailMessage = new MailMessage(); - - // sender - if (settings.ContainsKey("SMTPRelay") && settings["SMTPRelay"] == "True" && !string.IsNullOrEmpty(notification.FromEmail)) + // get sender and receiver information from user object if not provided + if ((string.IsNullOrEmpty(notification.FromEmail) || string.IsNullOrEmpty(notification.FromDisplayName)) && notification.FromUserId != null) { - if (!string.IsNullOrEmpty(notification.FromDisplayName)) + var user = userRepository.GetUser(notification.FromUserId.Value); + if (user != null) { - mailMessage.From = new MailAddress(notification.FromEmail, notification.FromDisplayName); + notification.FromEmail = (string.IsNullOrEmpty(notification.FromEmail)) ? user.Email : notification.FromEmail; + notification.FromDisplayName = (string.IsNullOrEmpty(notification.FromDisplayName)) ? user.DisplayName : notification.FromDisplayName; + } + } + if ((string.IsNullOrEmpty(notification.ToEmail) || string.IsNullOrEmpty(notification.ToDisplayName)) && notification.ToUserId != null) + { + var user = userRepository.GetUser(notification.ToUserId.Value); + if (user != null) + { + notification.ToEmail = (string.IsNullOrEmpty(notification.ToEmail)) ? user.Email : notification.ToEmail; + notification.ToDisplayName = (string.IsNullOrEmpty(notification.ToDisplayName)) ? user.DisplayName : notification.ToDisplayName; + } + } + + // validate recipient + if (string.IsNullOrEmpty(notification.ToEmail)) + { + log += "Recipient Missing For NotificationId: " + notification.NotificationId + "
"; + notification.IsDeleted = true; + notificationRepository.UpdateNotification(notification); + } + else + { + MailMessage mailMessage = new MailMessage(); + + // sender + if (settings.ContainsKey("SMTPRelay") && settings["SMTPRelay"] == "True" && !string.IsNullOrEmpty(notification.FromEmail)) + { + if (!string.IsNullOrEmpty(notification.FromDisplayName)) + { + mailMessage.From = new MailAddress(notification.FromEmail, notification.FromDisplayName); + } + else + { + mailMessage.From = new MailAddress(notification.FromEmail); + } } else { - mailMessage.From = new MailAddress(notification.FromEmail); + mailMessage.From = new MailAddress(settings["SMTPSender"], (!string.IsNullOrEmpty(notification.FromDisplayName)) ? notification.FromDisplayName : site.Name); + } + + // recipient + if (!string.IsNullOrEmpty(notification.ToDisplayName)) + { + mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName)); + } + else + { + mailMessage.To.Add(new MailAddress(notification.ToEmail)); + } + + // subject + mailMessage.Subject = notification.Subject; + + //body + mailMessage.Body = notification.Body; + + // encoding + mailMessage.SubjectEncoding = System.Text.Encoding.UTF8; + mailMessage.BodyEncoding = System.Text.Encoding.UTF8; + mailMessage.IsBodyHtml = true; + + // send mail + try + { + client.Send(mailMessage); + sent++; + notification.IsDelivered = true; + notification.DeliveredOn = DateTime.UtcNow; + notificationRepository.UpdateNotification(notification); + } + catch (Exception ex) + { + // error + log += ex.Message + "
"; } } - else - { - mailMessage.From = new MailAddress(settings["SMTPSender"], (!string.IsNullOrEmpty(notification.FromDisplayName)) ? notification.FromDisplayName : site.Name); - } - - // recipient - if (!string.IsNullOrEmpty(notification.ToDisplayName)) - { - mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName)); - } - else - { - mailMessage.To.Add(new MailAddress(notification.ToEmail)); - } - - // subject - mailMessage.Subject = notification.Subject; - - //body - mailMessage.Body = notification.Body; - - // encoding - mailMessage.SubjectEncoding = System.Text.Encoding.UTF8; - mailMessage.BodyEncoding = System.Text.Encoding.UTF8; - - // send mail - try - { - client.Send(mailMessage); - sent++; - notification.IsDelivered = true; - notification.DeliveredOn = DateTime.UtcNow; - notificationRepository.UpdateNotification(notification); - } - catch (Exception ex) - { - // error - log += ex.Message + "
"; - } } + log += "Notifications Delivered: " + sent + "
"; + } + else + { + log += "SMTP Not Configured Properly In Site Settings - Host, Port, SSL, And Sender Are All Required" + "
"; } - log += "Notifications Delivered: " + sent + "
"; } else { - log += "SMTP Not Configured Properly In Site Settings - Host, Port, SSL, And Sender Are All Required" + "
"; + log += "SMTP Disabled In Site Settings" + "
"; } }