using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using Microsoft.Extensions.DependencyInjection; using Oqtane.Models; using Oqtane.Repository; using Oqtane.Shared; namespace Oqtane.Infrastructure { public class NotificationJob : HostedServiceBase { // JobType = "Oqtane.Infrastructure.NotificationJob, Oqtane.Server" public NotificationJob(IServiceScopeFactory serviceScopeFactory) : base(serviceScopeFactory) { Name = "Notification Job"; Frequency = "m"; // minute Interval = 1; IsEnabled = false; } public override string ExecuteJob(IServiceProvider provider) { string log = ""; // iterate through tenants in this installation List tenants = new List(); var aliasRepository = provider.GetRequiredService(); List aliases = aliasRepository.GetAliases().ToList(); foreach (Alias alias in aliases) { if (tenants.Contains(alias.TenantId)) continue; tenants.Add(alias.TenantId); // use the SiteState to set the Alias explicitly so the tenant can be resolved var siteState = provider.GetRequiredService(); siteState.Alias = alias; // get services which require tenant resolution var siteRepository = provider.GetRequiredService(); var settingRepository = provider.GetRequiredService(); var notificationRepository = provider.GetRequiredService(); // iterate through sites for this tenant List sites = siteRepository.GetSites().ToList(); foreach (Site site in sites) { log += "Processing Notifications For Site: " + site.Name + "
"; // 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"] != "") { // construct SMTP Client var client = new SmtpClient() { 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 notifications int sent = 0; List notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList(); foreach (Notification notification in notifications) { MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(settings["SMTPSender"], site.Name); mailMessage.Subject = notification.Subject; if (notification.FromUserId != null) { mailMessage.Body = "From: " + notification.FromDisplayName + "<" + notification.FromEmail + ">" + "\n"; } else { mailMessage.Body = "From: " + site.Name + "\n"; } mailMessage.Body += "Sent: " + notification.CreatedOn + "\n"; if (notification.ToUserId != null) { mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName)); mailMessage.Body += "To: " + notification.ToDisplayName + "<" + notification.ToEmail + ">" + "\n"; } else { mailMessage.To.Add(new MailAddress(notification.ToEmail)); mailMessage.Body += "To: " + notification.ToEmail + "\n"; } mailMessage.Body += "Subject: " + notification.Subject + "\n\n"; mailMessage.Body += notification.Body; // send mail try { client.Send(mailMessage); sent = 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" + "
"; } } } return log; } private Dictionary GetSettings(List settings) { Dictionary dictionary = new Dictionary(); foreach (Setting setting in settings.OrderBy(item => item.SettingName).ToList()) { dictionary.Add(setting.SettingName, setting.SettingValue); } return dictionary; } } }