From 5a021187ccf694033629b551cb2d0457f5705617 Mon Sep 17 00:00:00 2001 From: Thomas Ryan Date: Sat, 16 Nov 2024 00:33:13 -0800 Subject: [PATCH] Added a try catch for when the email server breaks. --- Messaging/TimedHostedService.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Messaging/TimedHostedService.cs b/Messaging/TimedHostedService.cs index 8f2d0e38..ec60fc77 100644 --- a/Messaging/TimedHostedService.cs +++ b/Messaging/TimedHostedService.cs @@ -47,14 +47,22 @@ private async Task DoWork(AppSettings appSettings, CancellationToken cls) .Options; using var dbContext = new MessagingContext(contextOptions); - // Check for emails - var emails = await EmailMessage.GetEmailsAsync(appSettings.ConnectionStrings.EmailUsername, appSettings.ConnectionStrings.EmailPassword, cls); - // Transform each email to a text message - foreach (var email in MemoryMarshal.ToEnumerable(emails)) + try { - await EmailToForwardedMessageAsync(email, dbContext); - _logger.LogInformation("[Background Worker] [EmailToText] Forwarded message To {to} From {from}", email.To, email.From); + // Check for emails + var emails = await EmailMessage.GetEmailsAsync(appSettings.ConnectionStrings.EmailUsername, appSettings.ConnectionStrings.EmailPassword, cls); + // Transform each email to a text message + foreach (var email in MemoryMarshal.ToEnumerable(emails)) + { + await EmailToForwardedMessageAsync(email, dbContext); + _logger.LogInformation("[Background Worker] [EmailToText] Forwarded message To {to} From {from}", email.To, email.From); + } } + catch (Exception ex) { + + _logger.LogInformation("[Background Worker] [EmailToText] Failed to get new email messages."); + } + // Send the messages outbound _logger.LogInformation("[Background Worker] Timed Hosted Service has completed."); }