Fusonic.Extensions.Email 11.0.0

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

Email

Package: Fusonic.Extensions.Email

Renders emails from Razor views (.cshtml) or Razor components (.razor) and sends them via SMTP (MailKit). Emails are sent with the Mediator command SendEmail.

Setup

All types are in the namespace Fusonic.Extensions.Email.

Prerequisites

The package renders emails with the ASP.NET Core Razor engine. Register the following framework services in your IServiceCollection:

services.AddLogging();
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddRazorPages().AddViewLocalization(); // Razor views: IRazorViewEngine, ITempDataProvider, IViewLocalizer
services.AddRazorComponents();                  // Razor components
services.AddScoped<HtmlRenderer>();             // Microsoft.AspNetCore.Components.Web

AddMvc() or AddControllersWithViews() work as well, as long as view localization is enabled.

The package also needs an IMediator from Fusonic.Extensions.Mediator to send SendEmail.

Microsoft.Extensions.DependencyInjection

using Fusonic.Extensions.Email;
using Fusonic.Extensions.Mediator;
using Fusonic.Extensions.Mediator.DependencyInjection;

services.AddScoped<IMediator>(sp => new ServiceProviderMediator(sp));
services.AddEmail(options => configuration.GetSection("Email").Bind(options));

AddEmail registers the SendEmail handler. If you use the out-of-band decorators of Fusonic.Extensions.Hangfire, call AddEmail before AddOutOfBandDecorators.

SimpleInjector

RegisterEmail resolves IViewLocalizer and IStringLocalizerFactory from the container, so the framework services must be cross-wired:

using Fusonic.Extensions.Email;
using Fusonic.Extensions.Mediator;

container.RegisterEmail(options => configuration.GetSection("Email").Bind(options));
container.RegisterMediator(services, [typeof(Program).Assembly]);

services.AddSimpleInjector(container, options => options.AutoCrossWireFrameworkComponents = true);
// After building the service provider:
app.Services.UseSimpleInjector(container);

RegisterEmail registers the SendEmail handler itself; you don't need to pass the email assembly to RegisterMediator. With the out-of-band decorators of Hangfire, call RegisterEmail after RegisterOutOfBandDecorators.

Options

EmailOptions are validated on registration. A ValidationException is thrown if they are invalid.

Option Description
SenderAddress Required. Sender email address.
SenderName Sender display name.
SmtpServer Required, unless StoreInDirectory is set.
SmtpPort Default 465.
EnableSsl Default true.
SmtpUsername, SmtpPassword SMTP credentials.
StoreInDirectory Writes emails to this directory instead of sending them (development only). SMTP settings are ignored.
SubjectPrefix Prefixes every subject, e.g. "[MyBranch] " for review environments.
BccAddress BCC address for every email. A BccRecipient set on SendEmail has precedence.
CssPath CSS file used for inlining. Default [assemblyDir]/wwwroot/assets/emails/email.css. null disables CSS.
DefaultHeaders Headers added to every email, see Headers.
IgnoredDomains Recipients with these domains are not sent an email. The domain invalid is always ignored.
RazorContentPostProcessor, BlazorContentPostProcessor See Post Processor.

Example appsettings.json:

{
    "Email": {
        "SenderAddress": "noreply@example.com",
        "SenderName": "Example",
        "SmtpServer": "smtp.example.com",
        "SmtpUsername": "user",
        "SmtpPassword": "secret"
    }
}

Send an email

Send the SendEmail command with a view model (Razor view) or component model (Razor component):

await mediator.Send(new SendEmail(
    Recipient: "jane@example.com",
    RecipientDisplayName: "Jane Doe",
    Culture: new CultureInfo("de-AT"),
    ViewModel: new RegistrationEmailViewModel { Name = "Jane" }));

Optional parameters:

  • SubjectKey: overrides the subject key of the view/component.
  • SubjectFormatParameters: format parameters for the translated subject (string.Format(subject, parameters)).
  • BccRecipient, ReplyTo: additional addresses.
  • Attachments: see Attachments.
  • Headers: see Headers.

The email is rendered in the given Culture.

Sending in the background

The handler of SendEmail is marked with [OutOfBand]. With the out-of-band decorators of Fusonic.Extensions.Hangfire registered, the email is sent in a Hangfire background job. Without them, it is sent synchronously.

Razor Views (MVC)

  1. Add a view (.cshtml), e.g. Views/Emails/Registration.cshtml.
  2. Add a view model and mark it with [EmailView]. The argument is the path to the view, relative to the Views directory, without the .cshtml extension:
    [EmailView("Emails/Registration")]
    public class RegistrationEmailViewModel
    {
        public required string Name { get; init; }
    }
    
  3. Send SendEmail with an instance of the view model.

View locations

The Razor view engine looks in the folder Views by default. A view path of Emails/FancyEmail matches Views/Emails/FancyEmail.cshtml.

To place the views in other folders, for example /Emails, configure the Razor options:

services.Configure<RazorViewEngineOptions>(options => options.ViewLocationFormats.Add("/Emails/{0}" + RazorViewEngine.ViewExtension));

Set a Subject

By default the subject key is Subject. It is resolved with the view localizer from the resource file of the view. To use a different key, set it on the attribute: [EmailView("Emails/Registration", SubjectKey = "RegistrationSubject")]. If no translation is found, the key itself is used as subject.

Razor Components (Blazor)

  1. Add a component (.razor). It can be placed in any folder or assembly.
  2. Add a component model implementing IComponentModel<T> (namespace Fusonic.Extensions.AspNetCore.Blazor), where T is the type of the component:
    public class RegistrationEmailModel : IComponentModel<RegistrationEmail>
    {
        public required string Name { get; init; }
    }
    
  3. Send SendEmail with an instance of the component model as ViewModel.

Set a Subject

By default the subject key is Subject. It is resolved from the resource file of the component. To use a different key, implement IProvideEmailSubject on the component model: string IProvideEmailSubject.SubjectKey => "RegistrationSubject";. If no translation is found, the key itself is used as subject.

Attachments

Pass attachments as Attachment(Name, Source) to SendEmail. By default only file:// URIs are supported. To load attachments from other sources (e.g. AWS S3), implement IEmailAttachmentResolver and register it:

// SimpleInjector
container.Collection.Append<IEmailAttachmentResolver, YourResolver>();

// Microsoft.Extensions.DependencyInjection
services.AddSingleton<IEmailAttachmentResolver, YourResolver>();

Headers

No special headers are sent by default.

  • EmailOptions.DefaultHeaders sets headers for all emails.
  • The Headers parameter of SendEmail adds headers to a single email. Headers with the same name as a default header replace it; all other default headers are still sent.

Predefined sets of headers can be found in EmailHeaders:

Post Processor

EmailOptions.RazorContentPostProcessor (.cshtml) and EmailOptions.BlazorContentPostProcessor (.razor) process the rendered HTML before it is sent. By default both inline the CSS into the tags (CssInliner.Inline). Set them to null to disable post processing.

The processors get a RazorPostProcessContext (Html, ViewModel, EmailViewAttribute) or a BlazorPostProcessContext (Html, ComponentModel) and return the final HTML.

Mjml Sample

With the following options, all Blazor emails are processed by Mjml.Net before being sent:

var mjmlRenderer = new MjmlRenderer();

services.AddEmail(o => // or container.RegisterEmail
{
    // Other configurations...

    o.BlazorContentPostProcessor = async ctx =>
    {
        var result = await mjmlRenderer.RenderAsync(ctx.Html);
        return result.Html;
    };
});
Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
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
11.0.0 0 11/23/2026
10.3.0 112 11/9/2026
10.2.0 115 11/5/2026
10.1.0 1,394 3/18/2026
10.0.1 164 3/17/2026
10.0.0 432 11/25/2025
10.0.0-rc.2 214 11/24/2025
10.0.0-rc.1 184 9/12/2025
9.5.4 395 8/20/2025
9.5.3 371 3/28/2025
9.5.2 429 1/16/2025
9.5.0 417 10/4/2024
9.4.0 345 9/23/2024
9.3.1 403 9/11/2024
9.3.0 292 9/11/2024
9.2.0 327 8/8/2024
9.2.0-rc.3 140 8/7/2024
9.2.0-rc.2 125 8/5/2024
9.2.0-rc.1 142 8/1/2024
9.1.0 271 7/29/2024
Loading failed