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:
| Name | Neutral Value (de) | en |
| Subject | Hallo {0} | Hello {0} |
| WelcomeText | Willkommen zum Newsletter! | Welcome to the Newsletter! |
| SubscriptionText | Danke, 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.

