DotNetExtras.Mail 1.0.1

dotnet add package DotNetExtras.Mail --version 1.0.1
                    
NuGet\Install-Package DotNetExtras.Mail -Version 1.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="DotNetExtras.Mail" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotNetExtras.Mail" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="DotNetExtras.Mail" />
                    
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 DotNetExtras.Mail --version 1.0.1
                    
#r "nuget: DotNetExtras.Mail, 1.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 DotNetExtras.Mail@1.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=DotNetExtras.Mail&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=DotNetExtras.Mail&version=1.0.1
                    
Install as a Cake Tool

DotNetExtras.Mail

DotNetExtras.Mail is a .NET Core library that simplifies finding, loading, and transforming localized email templates.

Use the DotNetExtras.Mail library to:

  • Pick the localized version of an email template for the specified language and culture.
  • Find an alternative template if the specified language is not available.
  • Merge localized templates with message-specific data.
  • Allow localization of the email subjects along with the body text.
  • Retrieve email subject from the merged template.

Notice that this library does not send emails. It only provides functionality to prepare email templates for sending.

Usage

Let's assume that there are localized email templates for a notification identified by the template ID (Zodiac) and language extensions:

Zodiac/
  Zodiac_en-US.html
  Zodiac_en-GB.html
  Zodiac_es.html
  Zodiac_fr.html
  Zodiac_pt.html
  ...

The templates use the Razor syntax and the Model placeholders, such as @Model.Name or @Raw(Model.Name) for text substitutions. The following code will try to load the es-MX version of the template and merge the specified template or the best alternative with the provided data:

use DotNetExtras.Mail;
...

// Data object to be merged with the template.
Data data = new()
{
    Zodiac = "Leo",
    Name = "John",
    Year = 2025
};

// Use the defaults in the constructor.
MailTemplate template = new();

// Load the 'es-MX' version of the 'Zodiac' email notification template 
// from the 'Samples/Zodiac' folder and merge it with the provided data.
// If the 'es-MX' version is not available, it will fall back to the 'es' translation.
// If the 'es' translation is also not available, it will fall back to the default template
// based on whatever default language suffix was defined previously.
template.Load("Samples/Zodiac", "Zodiac", "es-MX", ".html", data);

If the template was loaded successfully, the template object will contain the merged data. You can access the merged subject and body text as follows:

// The template object's 'Subject' property will hold the merged value 
// of the file template 'title' element.
string subject = template.Subject;

// The template object's 'Body' property will hold the merged value 
// of the file template's 'body' element.
string body = template.Body;

// The template object's 'Language' property will hold the language code 
// actually used by the template.
string language = template.Language;

For the complete examples including the email templates, see the unit tests project.

Documentation

For complete documentation, usage details, and code samples, see:

Package

Install the latest version of the DotNetExtras.Mail NuGet package from:

Resources

To simplify the process of building localized email templates, see:

See also

Check out other DotNetExtras libraries at:

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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
1.0.1 32 8/15/2025
1.0.0 85 8/11/2025