public void FlushQueue(DatabaseContext dbContext) { OrderNotificationLogRepository notificationRepo = new OrderNotificationLogRepository(dbContext); if (this._mailQueue.Count > 0) { using (SmtpClient smtpClient = new SmtpClient(this.hostname) { Port = this.smtpPort, UseDefaultCredentials = false, EnableSsl = true, Credentials = new NetworkCredential(this.username, this.password) }) { while (this._mailQueue.Count > 0) { EmailNotification notification = this._mailQueue.Dequeue(); try { // Send the mail message and log the results in the DB MailMessage msg = new MailMessage(); msg.To.Add(notification.To); msg.Body = notification.Message.Content; msg.Subject = notification.Message.Subject; msg.From = new MailAddress(this.fromaddress); msg.ReplyToList.Add(new MailAddress(this.replyToAddress)); msg.IsBodyHtml = true; smtpClient.Send(msg); // Log a success notificationRepo.LogNotification(notification.Order.OrderThumbprint, notification.To, true, notification.Message.ForCustomer, notification.Message.ForOrderDesk, string.Empty); } catch (Exception ex) { // Log a failure notificationRepo.LogNotification(notification.Order.OrderThumbprint, notification.To, false, notification.Message.ForCustomer, notification.Message.ForOrderDesk, ex.Message); } } } } }
static void Main(string[] args) { IConfiguration configuration = new ConfigurationBuilder() .AddJsonFile($"appsettings.json", true, true) .AddEnvironmentVariables() .Build(); string keyvault_endpoint = configuration["KEYVAULT_ENDPOINT"]; if (!string.IsNullOrEmpty(keyvault_endpoint)) { ConsoleWrite("Loading configuration from Azure Key Vault: " + keyvault_endpoint); var azureServiceTokenProvider = new AzureServiceTokenProvider(); var keyVaultClient = new KeyVaultClient( new KeyVaultClient.AuthenticationCallback( azureServiceTokenProvider.KeyVaultTokenCallback)); configuration = new ConfigurationBuilder() .AddConfiguration(configuration) .AddAzureKeyVault(keyvault_endpoint, keyVaultClient, new DefaultKeyVaultSecretManager()) .Build(); } IConfigurationSection smtpConfig = configuration.GetSection("SMTP"); DatabaseContext dbContext = new DatabaseContext(configuration.GetConnectionString(RunnerSettings.ConnectionStringName)); ConsoleWrite("EmailRunner starting main loop..."); // Program loop start while (true) { OrderRepository orderRepo = new OrderRepository(dbContext); OrderNotificationLogRepository orderNotificationRepo = new OrderNotificationLogRepository(dbContext); ConsoleWrite(" Loading orders..."); // Find order ids requiring customer emails List <string> OrdersNeedingCustomerNotifications = orderNotificationRepo.GetOrdersNeedingCustomerNotifications(); // Find order ids requiring order desk emails List <string> OrdersNeedingOrderDeskNotifications = orderNotificationRepo.GetOrdersNeedingOrderDeskNotifications(); // Load the order objects so we can use them later List <string> RequiredOrders = new List <string>(); RequiredOrders.AddRangeUnique(OrdersNeedingCustomerNotifications); RequiredOrders.AddRangeUnique(OrdersNeedingOrderDeskNotifications); Dictionary <string, Order> _orderCache = orderRepo.Get(RequiredOrders).ToDictionary(x => x.OrderThumbprint); EmailHelper email = new EmailHelper(smtpConfig["hostname"], smtpConfig["port"].ToInt(), smtpConfig["username"], smtpConfig["password"], smtpConfig["fromaddress"], smtpConfig["replytoaddress"]); string helpDeskEmailAddress = smtpConfig["HelpDeskEmail"]; ConsoleWrite("Found " + RequiredOrders.Count + " orders requiring email notifications"); // Enqueue order desk emails foreach (string t in OrdersNeedingOrderDeskNotifications) { if (_orderCache.ContainsKey(t)) { Order thisOrder = _orderCache[t]; // Only send emails to orders with email addresses if (helpDeskEmailAddress.Length > 0) { email.NewMessage(helpDeskEmailAddress, CannedEmailMessage.OrderDeskNotification(thisOrder), thisOrder); ConsoleWrite("Enqueueing Order Desk email for order: " + thisOrder.OrderThumbprint); } } } // Enqueue customer emails foreach (string t in OrdersNeedingCustomerNotifications) { if (_orderCache.ContainsKey(t)) { Order thisOrder = _orderCache[t]; // Only send emails to orders with email addresses if (thisOrder.CustomerEmailAddress.Length > 0) { email.NewMessage(thisOrder.CustomerEmailAddress, CannedEmailMessage.CustomerOrderThanks(thisOrder), thisOrder); ConsoleWrite("Enqueueing Customer email for order: " + thisOrder.OrderThumbprint); } } } // Send all emails ConsoleWrite("Sending " + (OrdersNeedingCustomerNotifications.Count + OrdersNeedingOrderDeskNotifications.Count) + " emails..."); email.FlushQueue(dbContext); ConsoleWrite("Done!"); ConsoleWrite("Sleeping for " + sleepTimeMinutes + " minutes..."); // Sleep for 15 minutes Task.Delay(sleepTimeMinutes * 60 * 1000).Wait(); } }