Mindfire.RazorEmail
1.0.1
dotnet add package Mindfire.RazorEmail --version 1.0.1
NuGet\Install-Package Mindfire.RazorEmail -Version 1.0.1
<PackageReference Include="Mindfire.RazorEmail" Version="1.0.1" />
<PackageVersion Include="Mindfire.RazorEmail" Version="1.0.1" />
<PackageReference Include="Mindfire.RazorEmail" />
paket add Mindfire.RazorEmail --version 1.0.1
#r "nuget: Mindfire.RazorEmail, 1.0.1"
#:package Mindfire.RazorEmail@1.0.1
#addin nuget:?package=Mindfire.RazorEmail&version=1.0.1
#tool nuget:?package=Mindfire.RazorEmail&version=1.0.1
Mindfire.RazorEmail
Culture-aware Razor email templates loaded from the filesystem, with a fluent send API backed by MailKit.
Each message is a folder of Razor files. The HTML body, the plain-text body, and the subject are chosen for the culture you pass in, then fall back toward a more general culture and finally to the default files.
Quick start
services.AddRazorEmail(configuration);
services.AddRazorEmailMailKit();
await generator
.Create("Welcome")
.WithModel(model)
.WithCulture("es-MX")
.To(user.Email, user.DisplayName)
.SendAsync();
AddRazorEmail registers rendering. AddRazorEmailMailKit registers the SMTP transport. Register another IEmailTransport instead when mail should go through a different provider.
Settings
FromAddress is required. SmtpHost is required after AddRazorEmailMailKit(). Username and password are optional. Leave SmtpPassword empty in source control and load it from user secrets or the environment.
{
"RazorEmail": {
"FromName": "Your App",
"FromAddress": "noreply@example.com",
"SmtpHost": "smtp.example.com",
"SmtpPort": 587,
"SmtpUseSsl": true,
"SmtpUsername": "",
"SmtpPassword": ""
}
}
TemplatesPath is optional and defaults to email. You do not need to set it for the default layout. The library always searches a project folder named EmailTemplates as well. The first folder that contains a complete message wins, so a copy under TemplatesPath overrides the same message name under EmailTemplates.
A PackageReference links appsettings.RazorEmail.example.json and RazorEmail.schema.json into the project. Those files are not loaded by the host. Copy the RazorEmail section into appsettings.json. Missing required keys fail at startup.
Where templates are found
For each message name, roots are searched in this order:
RazorEmail:TemplatesPath(defaultemail), relative to the content root, the app base directory, and the working directory.EmailTemplatesin those same places.- Any
FallbackTemplateRootsyou add in code.
A message folder is complete when it has a resolvable HTML body, a resolvable plain-text body, and email-settings.json with a Subject. The first complete folder is used. Later roots are not consulted for that message.
Mindfire.RazorEmail.Identity copies its default messages into EmailTemplates on the first build, when that folder is not already there. Files already in the folder are left alone.
RazorLight reads the files from disk. They have to be present in the directory the app uses as its content root. During dotnet run that is the project directory. A published app uses the publish directory, so the files also need to be copied to the build output. The Identity package does that for EmailTemplates. A folder you add yourself under email needs the same treatment, or the published app will not see it:
<ItemGroup>
<Content Remove="email\**\*" />
<RazorGenerate Remove="email\**\*" />
<RazorCompile Remove="email\**\*" />
<None Include="email\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>
Content Remove and the Razor removes keep the SDK from compiling the .cshtml files as Blazor or MVC views.
Build a message
Create a folder whose name is the string you pass to Create. The folder name is the message type. It can contain spaces.
EmailTemplates/
Welcome/
email.cshtml
email.txt.cshtml
email-settings.json
Shared/
_Layout.cshtml
email-settings.json holds the default subject:
{
"Subject": "Welcome"
}
email.cshtml is the HTML body. email.txt.cshtml is the plain-text body. Both are required. Razor encodes output, so a URL written as @Model.StartUrl is safe inside HTML. Pass the raw URL in; do not pre-encode it.
@model WelcomeModel
@{
Layout = "Shared/_Layout.cshtml";
}
<p>Hello @Model.Name,</p>
<p><a href="@Model.StartUrl">Open your account</a></p>
@model WelcomeModel
Hello @Model.Name,
Open your account:
@Model.StartUrl
public sealed record WelcomeModel(string Name, string StartUrl);
The layout path is relative to the template root that contained the message folder (EmailTemplates/Shared/_Layout.cshtml when the message lives under EmailTemplates). A layout is optional. Omit the Layout assignment and the body file is the whole message.
Send it with the generator for that model type. RazorEmailGenerator<T> is registered for every model type when you call AddRazorEmail.
public sealed class WelcomeMail(RazorEmailGenerator<WelcomeModel> welcome)
{
public Task SendAsync(string email, string name, string startUrl, CancellationToken cancellationToken)
{
return welcome
.Create("Welcome")
.WithModel(new WelcomeModel(name, startUrl))
.WithCulture(CultureInfo.CurrentUICulture)
.To(email, name)
.SendAsync(cancellationToken);
}
}
WithCulture accepts a culture name or a CultureInfo. The Identity sender passes CultureInfo.CurrentUICulture for you.
Culture-specific files
Add a culture to the file name. Do not add another folder.
| File | Used for |
|---|---|
email.cshtml |
Default HTML |
email.txt.cshtml |
Default plain text |
email.es-MX.cshtml |
Spanish (Mexico) HTML |
email.es-MX.txt.cshtml |
Spanish (Mexico) plain text |
email.es.cshtml |
Spanish HTML, any region that does not have its own file |
email.es.txt.cshtml |
Spanish plain text |
Subjects use the same idea inside email-settings.json. Subject is the default. Subjects holds culture-specific lines. Keys are culture names.
{
"Subject": "Welcome",
"Subjects": {
"es": "Bienvenido",
"es-MX": "Bienvenido a tu cuenta"
}
}
HTML, plain text, and the subject are resolved separately. You can translate the HTML and leave the plain text on the default file, or translate the subject and leave both bodies on the default files.
Fallback
The culture you pass in is tried first, then each parent culture, then the default file or Subject.
For es-MX the order is:
es-MXes- The default
email.cshtml,email.txt.cshtml, orSubject
So a Mexican Spanish user gets email.es-MX.cshtml when that file exists. When it does not, the same user gets email.es.cshtml. When that file does not exist either, they get email.cshtml. The plain-text body and the subject walk the same chain on their own.
es-ES with only an es file also uses the es file, because es is the parent of es-ES. A culture with no matching file and no matching parent uses the default.
Requested es-MX
HTML email.es-MX.cshtml -> missing, so email.es.cshtml
Text email.es-MX.txt.cshtml -> missing, email.es.txt.cshtml missing, so email.txt.cshtml
Subject Subjects["es-MX"] -> missing, so Subjects["es"]
An unknown culture name that is not a real .NET culture is tried as that exact string, then the default. It does not walk a parent chain.
Call WithCulture("es-MX") to force a culture in a test or a preview. Omit WithCulture and the default files are used.
| 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
- MailKit (>= 4.17.0)
- Microsoft.Extensions.Caching.Memory (>= 10.0.10)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.10)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.10)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.10)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.10)
- RazorLight (>= 2.3.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Mindfire.RazorEmail:
| Package | Downloads |
|---|---|
|
Mindfire.RazorEmail.Identity
ASP.NET Core Identity email sender that renders Mindfire.RazorEmail templates, including default confirm-email and password-reset messages. |
GitHub repositories
This package is not used by any popular GitHub repositories.