Greetings again experts,
Send server status email to different groups
The code below checks status of apps whether they are down or up and sends email to certain executive team.
However, I have been tasked with modifying the code to send two different types of emails, one - send email to one group regardless of whether the apps running are DOWN or WORKING. This part works.
The second part is to send email to another group only when any or all of the apps are DOWN.
I have come up with the code to do this but not sure how to handle these two tasks in ONE code.
For instance, by adding this condition:
if (results.Any(up => !up))
{
--then continue with
StringBuilder body = ...
}
How do I combine this condition with the rest of the code so that one group of executives receive emails regardless of whether app is down or not and the other group receives email only when the app is down?
namespace showserverstatus
{
class Program
{
static async Task<int> Main(string[] args)
{
System.Collections.Concurrent.ConcurrentDictionary<string, bool> urlToStatus = new();
IEnumerable<Task<bool>> tasks = args.Select(async url =>
{
bool result = await ServerStatusByAsync(url);
return urlToStatus.TryAdd(url, result);
});
bool[] results = await Task.WhenAll(tasks);
StringBuilder body = new("<p>Please find the status of the DMZ servers below:</p>");
body.Append("<ul>");
foreach (var kvp in urlToStatus)
{
string encodedLink = System.Net.WebUtility.HtmlEncode(kvp.Key);
body.Append(kvp.Value ? "<li>" : "<li style=\"color:red;background-color:yellow;\">");
body.Append(kvp.Value ? "<a href=\"" : "<a style=\"color:red;\" href=\"");
body.Append(encodedLink);
body.Append("\">");
body.Append(encodedLink);
body.Append("</a> - ");
body.Append(kvp.Value ? "WORKING" : "DOWN");
body.Append("</li>");
}
body.Append("</ul>");
await SendEmailAsync("DMZ Server Status", body.ToString(), true);
await Task.Delay(3000);
// Return the number of servers which were down:
return results.Count(result => !result);
}
static async Task<bool> ServerStatusByAsync(string url)
{
HttpClient http = new();
using (HttpResponseMessage response = await http.GetAsync(url))
{
Console.WriteLine("GET {0}: {1}", url, response.StatusCode);
if (response.IsSuccessStatusCode)
{
// await SendEmailAsync($"{url} WORKING", $"GET {url} returned {response.StatusCode}");
return true;
}
//await SendEmailAsync($"{url} DOWN", $"GET {url} returned {response.StatusCode}");
return false;
}
}
static async Task SendEmailAsync(string subject, string body, bool isBodyHtml = false)
{
using MailMessage mm = new(ConfigurationManager.AppSettings["FromEmail"], "joeblow@gmail.com");
mm.To.Add("janedoe@tahoo.com");
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = isBodyHtml;
SmtpClient smtp = new()
{
Host = ConfigurationManager.AppSettings["Host"],
Port = int.Parse(ConfigurationManager.AppSettings["Port"]),
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]),
};
await smtp.SendMailAsync(mm);
}
}
}
I hope my concern is clear.
As always, thanks in advance.