BlazorMail 0.0.1
dotnet add package BlazorMail --version 0.0.1
NuGet\Install-Package BlazorMail -Version 0.0.1
<PackageReference Include="BlazorMail" Version="0.0.1" />
<PackageVersion Include="BlazorMail" Version="0.0.1" />
<PackageReference Include="BlazorMail" />
paket add BlazorMail --version 0.0.1
#r "nuget: BlazorMail, 0.0.1"
#:package BlazorMail@0.0.1
#addin nuget:?package=BlazorMail&version=0.0.1
#tool nuget:?package=BlazorMail&version=0.0.1
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
.razorcomponent to HTML, no per-type boilerplate - Automatic CSS inlining —
<style>rules are moved to inlinestyle=""attributes (via AngleSharp) - Template abstraction —
IEmailTemplateencapsulates 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.csprojfor Blazor'sHtmlRendererto 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:
- The Blazor component is rendered to raw HTML via
HtmlRenderer - AngleSharp parses the document and reads all
<style>blocks - Each CSS rule is matched against the document's elements and applied as an inline
style=""attribute - Existing inline styles take priority over stylesheet rules (standard CSS specificity)
<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 | Versions 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. |
-
net10.0
- AngleSharp (>= 1.4.0)
- AngleSharp.Css (>= 1.0.0-beta.213)
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 |