NPv.Mail.Abstractions
2.2.0
dotnet add package NPv.Mail.Abstractions --version 2.2.0
NuGet\Install-Package NPv.Mail.Abstractions -Version 2.2.0
<PackageReference Include="NPv.Mail.Abstractions" Version="2.2.0" />
<PackageVersion Include="NPv.Mail.Abstractions" Version="2.2.0" />
<PackageReference Include="NPv.Mail.Abstractions" />
paket add NPv.Mail.Abstractions --version 2.2.0
#r "nuget: NPv.Mail.Abstractions, 2.2.0"
#:package NPv.Mail.Abstractions@2.2.0
#addin nuget:?package=NPv.Mail.Abstractions&version=2.2.0
#tool nuget:?package=NPv.Mail.Abstractions&version=2.2.0
NPv.Mail.Abstractions
A lightweight set of abstractions for email sending and templating in .NET applications.
π Breaking changes
v2.0.0
- Target framework updated to
net10.0(droppednet9.0support).
This is a personal library that focuses on the latest .NET runtime to keep maintenance simple and enjoyable.
β¨ Features
IMailSenderβ abstraction for sending emails with either application-level default SMTP settings or per-call SMTP settings.MailAttachmentβ DTO for file attachments.MailRequestβ DTO for email data, including optional file attachments.SmtpSettingsβ POCO for SMTP configuration, compatible withIConfigurationbinding and explicit per-message configuration.EmailContentβ DTO representing the rendered email content (subject and HTML body).IEmailTemplateModelβ interface for providing localization data for email templates.IEmailTemplateRendererβ abstraction for rendering HTML email templates with localization and dynamic data.
π¦ Installation
Add a reference to the NuGet package:
dotnet add package NPv.Mail.Abstractions
Or with the Package Manager Console:
Install-Package NPv.Mail.Abstractions
βοΈ Usage
Configure default SMTP settings
SmtpSettings is designed to be bound from configuration and reused by an IMailSender implementation as the default SMTP server.
builder.Services.Configure<SmtpSettings>(
builder.Configuration.GetSection("Smtp")
);
// Register your implementations
builder.Services.AddTransient<IMailSender, MailKitMailSender>();
builder.Services.AddTransient<IEmailTemplateRenderer, ScribanEmailTemplateRenderer>();
Send an email with default SMTP settings
Use this overload when the sender implementation should use SMTP settings configured at application startup.
var message = new MailRequest
{
To = "recipient@example.com",
Subject = "Welcome!",
HtmlBody = "<p>Hello world!</p>",
Attachments =
[
new MailAttachment
{
FileName = "hello.txt",
Content = Encoding.UTF8.GetBytes("Hello world!"),
ContentType = "text/plain"
}
]
};
await mailSender.SendAsync(message, CancellationToken.None);
Send an email with explicit SMTP settings
Use this overload when the SMTP server must be selected dynamically, for example for tenant-specific mailboxes, customer-specific sender accounts, or one-off delivery settings.
var smtpSettings = new SmtpSettings
{
ServerName = "smtp.customer.example.com",
ServerPort = 587,
UserName = "customer-sender@example.com",
Password = "smtp-password",
FromAddress = "customer-sender@example.com",
FromName = "Customer Sender"
};
var message = new MailRequest
{
To = "recipient@example.com",
Subject = "Welcome!",
HtmlBody = "<p>Hello from a custom SMTP server.</p>"
};
await mailSender.SendAsync(smtpSettings, message, CancellationToken.None);
π§© IMailSender contract
public interface IMailSender
{
Task SendAsync(SmtpSettings smtpSettings, MailRequest message, CancellationToken cancellationToken);
Task SendAsync(MailRequest message, CancellationToken cancellationToken);
}
SendAsync(MailRequest message, CancellationToken cancellationToken)sends through the implementation's default SMTP configuration.SendAsync(SmtpSettings smtpSettings, MailRequest message, CancellationToken cancellationToken)sends through the SMTP configuration provided for this specific call.
π Render a template
var content = emailTemplateRenderer.Render(new ConfirmEmailTemplateModel(confirmUrl, "en"));
Console.WriteLine(content.Subject);
Console.WriteLine(content.HtmlBody);
Configuration
"Smtp": {
"ServerName": "smtp.example.com",
"ServerPort": 587,
"UserName": "user@example.com",
"Password": "yourpassword",
"FromAddress": "noreply@example.com",
"FromName": "Example Sender"
}
π Author's Note
This library grew out of my long-standing personal interest in structuring and publishing open source packages. Over time, Iβve revisited and refined earlier internal utilities and ideas, giving them a more consistent shape and preparing them for wider reuse. Along the way, Iβve also taken the opportunity to explore how open source distribution and licensing work in the .NET ecosystem.
Itβs a small step toward something Iβve always wanted to try β sharing practical, minimal tools that reflect years of learning, experimentation, and refinement.
Hopefully, someone finds it useful.
Nikolai π
βοΈ License
MIT β you are free to use this in commercial and open-source software.
| 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
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on NPv.Mail.Abstractions:
| Package | Downloads |
|---|---|
|
NPv.Mail
SMTP and templated email sender implementation for NPv.Mail.Abstractions. Provides MailKit-based delivery, Scriban-based template rendering, and seamless configuration via Microsoft.Extensions.Options. Designed for clean architecture, DI integration, and testability. |
GitHub repositories
This package is not used by any popular GitHub repositories.