1. Install

Navigate to the project directory you want to create the templates in, and run

nuget
dotnet add package BlazorEmail

2. Dependency Injection

To register the renderer, add the following line in your Program.cs or Startup.cs:

builder.Services.AddTransient<BlazorEmailRenderer>();

Using AddTransient creates a new instance of the BlazorEmailRenderer each time it is requested, making it ideal for lightweight and stateless services.

3. Create your first email template

To create an email template, create a new file with the .razor type, eg. EmailTemplate.razor. Then do:

EmailTemplate.razor
@using BlazorEmail.Components

@inherits BlazorEmail.EmailBase

<Email>
    <Head Title="@EmailTitle"/>
    <Head>
        <title>@EmailTitle</title>
    </Head>
    <Body>
        Hello world!
    </Body>
</Email>

@code
{
    [Parameter, EditorRequired] public string? EmailTitle { get; set; }
}

4. Render template to HTML and send the email

In this example, the ExampleClass demonstrates how to render an email template to HTML and send it using the BlazorEmailRenderer and some email client.


public class ExampleClass
{
    private readonly BlazorEmailRenderer m_EmailRenderer;
    private readonly IEmailSender m_EmailSender;

    public ExampleClass(BlazorEmailRenderer emailRenderer, IEmailSender emailSender)
    {
        m_EmailRenderer = emailRenderer;
        m_EmailSender = emailSender;
    }

    public async Task SendEmail(string emailTitle)
    {
        var emailHtml = await m_EmailRenderer.RenderEmail<EmailTemplate>(emailParams =>
            emailParams.Add(x => x.EmailTitle, emailTitle));

        await m_EmailSender.Send(emailHtml, "test@test.com");
    }
}

It’s as simple as that