IC Improve Code Recipes

Chapter 9

E-mail and Notifications

E-mail is the integration every business application has and nobody designs. It starts as three lines in a button click handler that send a confirmation, and ten years later it is a static EmailHelper class that half the solution calls, with a hard-coded SMTP server name, a catch { } around the send, and a production incident every time the mail relay changes its TLS settings.

The archive behind this book shows how common that path is. E-mail code turned up in 28 engagements, with 2,507 e-mail-related classes. EmailHelper alone appears 342 times, the third most common helper class name after Utility and SQLHelper. About 1,040 hand-written C# files create a System.Net.Mail SmtpClient, spread across 25 engagements. Almost all of them send synchronously, on the request thread, with no retry.

This chapter replaces that pattern with a small set of parts that fit together. One recipe sends mail with MailKit, the library Microsoft's own documentation points to. Others build HTML messages with a plain-text fallback, handle attachments and inline images, queue messages so a slow relay never blocks a user, and test the whole thing without spamming a real inbox. Every recipe uses MailKit and its companion MIME library. MailKit 5 depends on MimeKit.Core, which NuGet restores for you; the code in this chapter also builds against MailKit 4.

Recipes

  1. 9.1Moving from SmtpClient to MailKitSystem.Net.Mail.SmtpClient still compiles, so old code keeps using it and new code copies the old code. Microsoft's documentation for the class says…
  2. 9.2HTML Templates with a Plain-Text FallbackNotification messages are built by string concatenation in C#, with HTML tags inside string literals. Changing a sentence needs a developer and a…
  3. 9.3Attachments and Inline ImagesReports, invoices and statements go out as attachments. The usual code reads the whole file into a byte array, attaches it with a name built from…
  4. 9.4Queueing and RetryingSending mail inline in a web request couples the user's experience to the mail relay. When the relay is slow, the page is slow. When the relay is…
  5. 9.5Testing Without Sending Real MailEvery team has a story about the test environment that e-mailed real customers. Mine involved a copy of production data on a staging server, a…

Chapter checklist

  • Search for new SmtpClient( and plan the move to MailKit behind an IEmailSender interface.
  • Move SMTP host, port, security mode and credentials into configuration, and the password into a secret store.
  • Delete every catch { } around a send. Let the queue or caller log and decide.
  • Send both an HTML part and a plain-text part, and HTML-encode every value placed in a template.
  • Cap total attachment size and send links for large or sensitive documents.
  • Queue mail from web requests and retry only 4xx and network failures; use an outbox table when a message must not be lost.
  • Configure every non-production environment to redirect or capture mail, with the safe mode as the default.