test

Sending HTML Templated Emails From .NET Core 2.0

A super simple way to sending email from your .NET Core applications

Sending HTML Templated Emails From .NET Core 2.0

A super simple way to sending email from your .NET Core applications

As of .NET Core 2.0 the SMTP Client and classes you’ll need to send email have been ported over to .NET Core. Here I’ll quickly show you how to send email from your .NET Core applications.

Sending Email From .NET Core 2.0

The first thing you need to do is ensure your project is .NET Core 2.0 or higher. If you’re not sure, check your .csproj file, you should see something like this: <TargetFramework>netcoreapp2.0</TargetFramework>

After you’re sure you’ve got a .NET Core 2.0 (or higher) project, the easiest way to send an email is to use the classes built in to the framework. Here is how you can send an email:

using (var mailMessage = new MailMessage())
using (var client = new SmtpClient(config.Email.SmtpServer, config.Email.SmtpPort))
{
    // configure the client and send the message
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential(config.Email.SmtpUser, config.Email.SmtpPassword);
    client.EnableSsl = true;

    // configure the mail message
    mailMessage.From = new MailAddress(config.Email.AlertEmailFrom);
    mailMessage.To.Insert(0, new MailAddress(config.Email.AlertEmailTo));
    mailMessage.Subject = config.Email.AlertEmailSubject;
    mailMessage.Body = body;
    mailMessage.IsBodyHtml = true;

    client.Send(mailMessage);
}

Both MailMessage and SmtpClient implement IDisposable so it is a good idea to wrap the emailing logic in a couple using blocks to clean up after the email has been sent. This code also assumes that we’ve got some objects on our configuration that contain the smtp server, port, credentials, and more. That’s all it takes to send an email!

Building An HTML Email Template

Creating Email Templates can be a pain. To get the best compatibility you have to do crazy things like table layouts and inline styles. Long ago I decided to maintain the template & styles outside of the .NET project. For this I like to use a tool which allows you to create a simple HTML template and style sheet and then mash them together into something that will display nicely on (most) email clients.

To accomplish this I usually use Inky (now “Foundation for Emails 2”). However lately I’ve been considering using Heml for my new template development work (mostly because I can eliminate the Gulp script I’d used to process templates and styles with Inky).

With either tool the end result is one HTML file that has inline styles and is ready to be put in my .NET project.

Using Variables In Your Template

Prior to .NET Core I liked using a package called RazorEngine which allows you to use the Razor Template Engine to process a template file into HTML that can be emailed. Unfortunately as of writing this it doesn’t work with .NET Core.

The easiest way I’ve found to handle replacing variables in my email templates with .NET Core is a StringBuilder.Replace(). In your email template you can specify varibales: {{foo-value}} and then using the StringBuilder.Replace() method you can quickly replace any variables in your template:

var builder = new StringBuilder();

using (var reader = File.OpenText("template.html"))
{
    builder.Append(reader.ReadToEnd());
}

builder.Replace("{{foo-value}}", "Bar!");

This doesn’t really give you ability to do if/then logic, etc., but it does allow you to personalize emails.

Conclusion

By now you should have a good idea how you can send emails with .NET Core 2.0 applications. I’ve also shown you a quick way to generate an HTML template and then replace variables in that email to make it more applicable to the recipient.