Email-Templating with Blazor

Dev Diary

Creating dynamic and reusable email components can be quite a challenging task. However, with the latest version of .NET this is a breeze to implement. Here are the few steps you need to take to get your generated emails to the next level.

Autor
Paul Hagspiel
Datum
31. Oktober 2024

Breaking Through Razor Pages Limitations

With great success, we have been using Razor Pages to render our emails (and even PDF files) for quite some time now. However, we more often than not hit some limitations with it in the past, which we are now able to overcome with the new HtmlRenderer in .NET 8. Just to list a few of the headaches it solved and advantages it brought us:
New emails within the same project are mostly composed of pre-existing components, which are quite hard to reuse with the old Razor Pages.
Sharing data between the Layout Page, Body Page, Partial View and a Tag Helper were neither safe nor generic.
The setup for rendering Blazor Components is far simpler than for Razor Pages. Moreover, it does not require a hard dependency on the ASP.NET Core Framework.
Blazor and thus Blazor Components seems to be the current and foreseeable focus of Microsoft for their templating engine.

»just show me the good stuff«

Before we dive right into the implementation, let’s first take a look at the experience of using our setup with a sample of a newsletter subscription email.

#1 Create localized texts

Obviously most applications are localized and thus the Emails should be as well. For that we will create two resource files NewsletterSubscription.resx and NewsletterSubscription.en.resx in a new Resources directory. The files should represent something like this: 

NameNeutral Value (de)en
SubjectHallo {0}Hello {0}
WelcomeTextWillkommen zum Newsletter!Welcome to the Newsletter!
SubscriptionTextDanke, dass Sie sich angemeldet haben {0}.Thanks for subscribing {0}.

#2 Create our Email

Create a NewsletterSubscription.razor file in a new Emails directory that will hold the body of our Email. 

Mehr davon?

Improving performance of inline-editable tables_B
Dev Diary
Improving performance of inline editable tables
29. Januar 2025 | 7 Min.
Kubernetes Dashboard Login über OpenID-Connect_B
Dev Diary
Kubernetes Dashboard Login mit OpenID-Connect
12. September 2024 | 7 Min.

Kontaktformular

*Pflichtfeld
*Pflichtfeld
*Pflichtfeld
*Pflichtfeld

Wir schützen deine Daten

Wir bewahren deine persönlichen Daten sicher auf und geben sie nicht an Dritte weiter. Mehr dazu erfährst du in unseren Datenschutzbestimmungen.