Skip to content

EmailSender Service using FluentEmail & Razor Templates

Sending emails is a very important functionality for any website nowdays, either to send account verification emails, newsletter emails or even notification emails.
Today I’ll be showing how you can create your own EmailSender Service in ASP.NET Core using FluentEmail and Razor templates for rich HTML emails.

EmailSender interface

If you’re working with a project that implements a Clean Architecture, you’ll want to create an interface in your Core project to define what you need the service to do, as well as to have the possibility to use it from the Core project too.

The method SendUsingTemplate has the following signature:

  • Task<bool>, the method is async and will return true if the email was successfully sent or false otherwise.
  • string to, the recipient email address.
  • string subject, the email’s subject/title.
  • EmailTemplate template, which template to use (enum).
  • object model, since most of the time you’ll want to send more information in the email, you can pass a model (or an anonymous object) to the template.
Info icon

If you noticed, the method doesn’t require a from email address, that’s because we’ll configure it in our Startup class since we usually only use one.

I use an enum for the template names to remove the chance of someone getting the template’s name wrong. Magic strings are always a bad idea.

EmailSender Implementation

Prerequesites

For the implementation of the service, you’ll only need the FluentEmail.Core NuGet package: Install-Package FluentEmail.Core

Implementation

The code is pretty straight-forward, we use FluentEmail’s IFluentEmail object (that gets injected) to send the email asynchronously.
I often prefer to log the errors in the service and only return a boolean response.
I don’t think the client needs to know why the email wasn’t sent, the developpers need to deal with that.

The only thing you’ll need to change is the TemplatePath constant, which needs to contain the full namespace name where your template files (.cshtml files) reside.

In order to be able to use anonymous objects in the templates, we’ll need to convert them into Expando objects. This is a “limitation” that RazorLight (the library that FluentEmail uses to handle Razor Templates) has.

The code might look alien but all it does is convert every anonymous object to an Expando object recursively (since anonymous objects can have anonymous objects…).

Razor Templates

Based on your TemplatePath value, you will need to put all your cshtml template files in that folder and they must be marked as: Embedded resource.

For example, this my ChangeEmail template file:

Properties of an email template file

Configuration

Prerequesites

When configuring the EmailSender service, you’ll need the following NuGet packages:

  • FluentEmail.Smtp
  • FluentEmail.Razor

Services

In this example I’m using a GMAIL account to send the emails, you can use whatever email server you want, even your own.

Conclusion

As you can see, creating an EmailSender service is pretty easy and straight-forward and can be used in all your projects. Having the ability to send rich HTML emails is a must for every website nowdays.

Published inASP.NET CoreProgramming

Leave a Reply

avatar
  Subscribe  
Notify of