private static async Task<List<Tuple<NotificationToSend, bool>>> SendNotificationsAsync(IEnumerable<NotificationToSend> notificationsToSend)
{
var tuples = new List<Tuple<NotificationToSend, bool>>();
using (var smtpClient = new SmtpClient())
{
foreach (var notification in notificationsToSend)
{
bool sentSuccessfully;
try
{
var mailMessage = new MailMessage
{
Subject = notification.Subject,
Body = $"{notification.Text} <br /> This notification was sent automatically",
IsBodyHtml = true
};
mailMessage.To.Add(notification.ToEmail);
await smtpClient.SendMailAsync(mailMessage);
sentSuccessfully = true;
}
catch (Exception e)
{
sentSuccessfully = false;
// Here I also plan to log the exception
}
var tuple = new Tuple<NotificationToSend, bool>(notification, sentSuccessfully);
tuples.Add(tuple);
}
}
return tuples;
}
try
{
await smtpClient.SendMailAsync(mailMessage);
sentSuccessfully = true;
}
catch (Exception e) when (e is ArgumentNullException ||
e is InvalidOperationException ||
e is ObjectDisposedException ||
e is SmtpException)
{
sentSuccessfully = false;
// Handle exception
}