BlazorMail 0.0.1

dotnet add package BlazorMail --version 0.0.1
                    
NuGet\Install-Package BlazorMail -Version 0.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlazorMail" Version="0.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BlazorMail" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="BlazorMail" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BlazorMail --version 0.0.1
                    
#r "nuget: BlazorMail, 0.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BlazorMail@0.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BlazorMail&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=BlazorMail&version=0.0.1
                    
Install as a Cake Tool

BlazorMail

A .NET 10 library for generating email HTML from Blazor components.

BlazorMail renders any Blazor component into a self-contained HTML string, with CSS automatically inlined — ready to be sent via any email provider.

Features

  • Generic Blazor renderer — render any .razor component to HTML, no per-type boilerplate
  • Automatic CSS inlining — <style> rules are moved to inline style="" attributes (via AngleSharp)
  • Template abstraction — IEmailTemplate encapsulates email metadata (subject, from, to) alongside rendering
  • Standard DI integration — one AddBlazorMail() call to register everything

Installation

<PackageReference Include="BlazorMail" Version="1.0.0" />

Important: The consuming project must also set <PreserveCompilationContext>true</PreserveCompilationContext> in its .csproj for Blazor's HtmlRenderer to work at runtime.

<PropertyGroup>
    <PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

Quick Start

1. Register the services

using BlazorMail.Extensions;

builder.Services.AddBlazorMail();

2. Create a Blazor component for your email


<!DOCTYPE html>
<html>
<head>
    <style>
        body { font-family: Arial, sans-serif; background: #f4f4f4; }
        .container { max-width: 600px; margin: 0 auto; background: white; padding: 30px; }
        .button { background: #667eea; color: white; padding: 12px 30px; text-decoration: none; border-radius: 6px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Welcome, @UserName!</h1>
        <p>Your account has been created successfully.</p>
        <a href="@DashboardUrl" class="button">Go to my dashboard</a>
    </div>
</body>
</html>

@code {
    [Parameter] public string UserName { get; set; } = "";
    [Parameter] public string DashboardUrl { get; set; } = "";
}

3. Implement IEmailTemplate

using BlazorMail.Rendering;
using BlazorMail.Templating;

public sealed class WelcomeEmailTemplate : IEmailTemplate
{
    public required string UserName { get; init; }
    public required string DashboardUrl { get; init; }

    public string EmailType => "WelcomeEmail";
    public required string Subject { get; init; }
    public required MailAddress From { get; init; }
    public required List<MailAddress> To { get; init; }

    public async Task<string> RenderAsync(IBlazorMailRenderer renderer)
    {
        return await renderer.RenderAsync<WelcomeEmail>(new()
        {
            { nameof(WelcomeEmail.UserName), UserName },
            { nameof(WelcomeEmail.DashboardUrl), DashboardUrl }
        });
    }
}

4. Render and send

Inject IBlazorMailRenderer directly, or use your IEmailTemplate implementation:

public class MyEmailService
{
    private readonly IBlazorMailRenderer _renderer;

    public MyEmailService(IBlazorMailRenderer renderer)
    {
        _renderer = renderer;
    }

    public async Task SendWelcomeAsync(string userName, string dashboardUrl)
    {
        WelcomeEmailTemplate template = new()
        {
            UserName = userName,
            DashboardUrl = dashboardUrl,
            Subject = "Welcome aboard!",
            From = new MailAddress("no-reply@myapp.com", "My App"),
            To = [ new MailAddress("user@example.com", userName) ]
        };

        // Generate the HTML
        string html = await template.RenderAsync(_renderer);

        // Send via your preferred email provider (SendGrid, Mailjet, SMTP, etc.)
        // await _emailProvider.SendAsync(template.From, template.To, template.Subject, html);
    }
}

API Reference

IBlazorMailRenderer

Task<string> RenderAsync<TComponent>(Dictionary<string, object?>? parameters = null)
    where TComponent : IComponent;

Renders a Blazor component to an HTML string with CSS inlined. parameters keys must match the component's [Parameter] property names exactly.

IEmailTemplate

Member Description
EmailType Identifier used for logging/monitoring (e.g. "WelcomeEmail")
Subject Email subject line
From Sender address
To List of recipient addresses
RenderAsync(renderer) Generates the HTML body

MailAddress

public record MailAddress(string Email, string? DisplayName = null);

A lightweight address record with no dependency on any specific email provider library.

AddBlazorMail()

services.AddBlazorMail();

Registers IBlazorMailRenderer as a transient service.

How CSS Inlining Works

BlazorMail uses AngleSharp to parse the rendered HTML and apply CSS rules inline:

  1. The Blazor component is rendered to raw HTML via HtmlRenderer
  2. AngleSharp parses the document and reads all <style> blocks
  3. Each CSS rule is matched against the document's elements and applied as an inline style="" attribute
  4. Existing inline styles take priority over stylesheet rules (standard CSS specificity)
  5. <style> blocks are removed from the final HTML

This ensures maximum email client compatibility (Outlook, Gmail, Apple Mail, etc.).

Requirements

  • .NET 10
  • The consuming project must be an ASP.NET Core application (or reference Microsoft.AspNetCore.App)
  • <PreserveCompilationContext>true</PreserveCompilationContext> in the consuming project
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.1 177 4/11/2026