Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor email templates in .net standard 2.1 / Core 3.1

I want to create a separate service for sending emails, this will be based on a .net core 3.1 application with its default layers (application, domain, contracts, infrastructure)

Within this project, I want to create a class library which holds my Razor templates and parses them so the application layer can send this as an email (both HTML and non-html) after the model has been applied, however this is where I run into a problem.

Every example I can find targets older .net core applications, which means nuget packages like RazorEngine.NetCore won't work.

Also examples as found in https://emilol.com/razor-mailer/ won't work as they target older frameworks.

A deep dive into the Razor Client Library also didn't work as I tried to adjust this sample for 3.1 https://derekarends.com/how-to-create-email-templates-with-asp-net-core-2-2/

Is there any way to make this work or do I really need to target an older framework just to create a email template library?

like image 965
Vincentw Avatar asked Nov 30 '25 14:11

Vincentw


1 Answers

I was looking for a simple solution for my Blazor application recently and found this GitHub project.: BlazorTemplater

It's really simple to use.

  1. Install the Nuget Package: BlazorTemplater
  2. Create your Razor file with parameters for your template:
    <p>Hello, @FirstName.</p>
    
    @code {
        [Parameter]
        public string FirstName { get; set; }
    }
    
  3. Render the Razor component to a string so that you can use it in the body of your email:
    private void SendWelcomeEmail()
    {
        string html = new ComponentRenderer<WelcomeEmailTemplate>()
            .Set(c => c.FirstName, Applicant.FirstName)
            .Render();
    
        Console.WriteLine(html);
    }
    
like image 178
paulpitchford Avatar answered Dec 04 '25 02:12

paulpitchford