Once upon a time, Ryan's company didn't use a modern logging framework to alert admins when services failed. No, they used everyone's favorite communications format, circa 2005: email. Can't reach the database? Send an email. Unhandled exception? Send an email. Handled exception? Better send an email, just in case. Sometimes they go to admins, sometimes they just go to an inbox used for logging.

Let's look at how that worked.

public void SendEMail(String receivers, String subject, String body)
{
    try
    {
        System.Net.Mail.SmtpClient clnt = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["SmtpServer"]);
        clnt.Send(new System.Net.Mail.MailMessage(
            ConfigurationManager.AppSettings["Sender"], 
            ConfigurationManager.AppSettings["Receivers"], 
            subject, 
            body));
    }
    catch (Exception ex)
    {
        SendEMail(
            ConfigurationManager.AppSettings["ErrorLogAddress"],
            "An error has occurred while sending an email",
            ex.Message + "\n" + ex.StackTrace);
    }
}

They use the Dot Net SmtpClient class to connect to an SMTP server and send emails based on the configuration. So far so good, but what happens when we can't send an email because the email server is down? We'll get an exception, and what do we do with it?

The same thing we do with every other exception: send an email.

Ryan writes:

Strangely enough, I've never heard of the service crashing or hanging. We must have a very good mail server!

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!