PosInformatique.Foundations.Emailing
1.0.0
Prefix Reserved
See the version list below for details.
dotnet add package PosInformatique.Foundations.Emailing --version 1.0.0
NuGet\Install-Package PosInformatique.Foundations.Emailing -Version 1.0.0
<PackageReference Include="PosInformatique.Foundations.Emailing" Version="1.0.0" />
<PackageVersion Include="PosInformatique.Foundations.Emailing" Version="1.0.0" />
<PackageReference Include="PosInformatique.Foundations.Emailing" />
paket add PosInformatique.Foundations.Emailing --version 1.0.0
#r "nuget: PosInformatique.Foundations.Emailing, 1.0.0"
#:package PosInformatique.Foundations.Emailing@1.0.0
#addin nuget:?package=PosInformatique.Foundations.Emailing&version=1.0.0
#tool nuget:?package=PosInformatique.Foundations.Emailing&version=1.0.0
PosInformatique.Foundations.Emailing
Introduction
PosInformatique.Foundations.Emailing provides a lightweight, template-based emailing infrastructure built on top of dependency injection.
It allows you to:
- Define strongly-typed email templates associated with data models.
- Instantiate emails from registered templates via an
IEmailManager. - Generate and send templated emails for each recipient through an
IEmailProviderimplementation.
The actual transport (SMTP, Azure Communication Service, Graph API, etc.) is delegated to a provider implementation. Existing implementation are available in the following packages:
- Azure Communication Service: PosInformatique.Foundations.Emailing.Azure.
- Microsoft Graph API: PosInformatique.Foundations.Emailing.Graph.
Install
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.Emailing
This package also depends on:
- PosInformatique.Foundations.EmailAddresses
- PosInformatique.Foundations.Text.Templating and one of its concrete implementations (for example PosInformatique.Foundations.Text.Templating.Razor or PosInformatique.Foundations.Text.Templating.Scriban).
Features
- Registration of email templates through
AddEmailing(...)andEmailingOptions.RegisterTemplate(...). - Strongly-typed template identifiers via
EmailTemplateIdentifier<TModel>. - Data models for templates based on any custom class.
- Template-based subject and HTML body using
TextTemplate<TModel>(e.g. Razor or Scriban). - Per-recipient data injection using a model with
EmailRecipient<TModel>. - Central
IEmailManagerto create and send emails. - Pluggable
IEmailProviderto send the finalEmailMessage(transport-agnostic design). - Support of the Importance of the e-mails (**Low, Normal and High).
Basic concepts
Email models
Each email template is associated with a data model. This model is injected into the subject and HTML body templates when generating the email content for a recipient.
public sealed class InvitationEmailTemplateModel
{
public string FirstName { get; set; } = string.Empty;
public string InvitationLink { get; set; } = string.Empty;
}
public sealed class AccountDeletionEmailTemplateModel
{
public string FirstName { get; set; } = string.Empty;
public DateTime DeletionDate { get; set; }
}
Template identifiers
Templates are registered and referenced through an EmailTemplateIdentifier<TModel>.
It is recommended to centralize them in a static class used by your business code:
public static class EmailTemplateIdentifiers
{
public static EmailTemplateIdentifier<InvitationEmailTemplateModel> Invitation { get; } =
EmailTemplateIdentifier<InvitationEmailTemplateModel>.Create();
public static EmailTemplateIdentifier<AccountDeletionEmailTemplateModel> AccountDeletion { get; } =
EmailTemplateIdentifier<AccountDeletionEmailTemplateModel>.Create();
}
Email templates
An EmailTemplate<TModel> is composed of two TextTemplate<TModel> instances:
- One for the subject.
- One for the HTML body.
These TextTemplate<TModel> come from PosInformatique.Foundations.Text.Templating, share the same model
instance during the e-mail generation process, and can be implemented using, for example:
- PosInformatique.Foundations.Text.Templating.Razor
- PosInformatique.Foundations.Text.Templating.Scriban
var invitationSubjectTemplate = new RazorTextTemplate<InvitationEmailTemplateModel>(typeof(InvitationSubjectRazorComponent));
var invitationHtmlBodyTemplate = new RazorTextTemplate<InvitationEmailTemplateModel>(typeof(InvitationBodyRazorComponent));
var invitationTemplate = new EmailTemplate<InvitationEmailTemplateModel>(
invitationSubjectTemplate,
invitationHtmlBodyTemplate);
Configuration
Register the emailing feature
You register the emailing feature in your IServiceCollection via AddEmailing(...).
In the options, you must at least configure:
- The sender email address.
- The mapping between
EmailTemplateIdentifier<TModel>andEmailTemplate<TModel>.
using Microsoft.Extensions.DependencyInjection;
using PosInformatique.Foundations.EmailAddresses;
using PosInformatique.Foundations.Emailing;
var services = new ServiceCollection();
services.AddEmailing(options =>
{
// Required: sender email address used for all outgoing emails
options.SenderEmailAddress = EmailAddress.Parse("no-reply@myapp.com");
// Register templates with their identifiers
options.RegisterTemplate(EmailTemplateIdentifiers.Invitation, invitationTemplate);
options.RegisterTemplate(EmailTemplateIdentifiers.AccountDeletion, accountDeletionTemplate);
});
Important: The
AddEmailing()method registers a scoped implementation ofIEmailManager. This is required because email templates (for example Razor-based templates) may depend on scoped services that are tied to the currently authenticated user. As a consequence, every service that depends onIEmailManagermust also be registered with a scoped lifetime.
The AddEmailing() method returns an EmailingBuilder that can be used to continue configuring the emailing infrastructure
(for example, provider registration in other packages).
Email providers
IEmailProvider is responsible for sending the final EmailMessage.
This package only defines the abstraction. A typical provider implementation is located in another package, such as:
See the PosInformatique.Foundations.Emailing.Azure or PosInformatique.Foundations.Emailing.Graph documentation for an example of provider registration.
Usage
1. Create an email from a template
To send an email, you first ask IEmailManager to create an Email<TModel> from a previously registered template identifier:
using PosInformatique.Foundations.Emailing;
var emailManager = serviceProvider.GetRequiredService<IEmailManager>();
// Create an email based on the "Invitation" template
var invitationEmail = emailManager.Create(EmailTemplateIdentifiers.Invitation);
invitationEmail.Importance = EmailImportance.High;
At this stage:
- The
Email<TModel>is linked to theEmailTemplate<TModel>previously registered inEmailingOptions. - No recipient has been added yet.
- The importance of the e-mail is defined to
EmailImportance.High.
2. Add recipients and models
You then populate the recipients collection with EmailRecipient<TModel>. Each recipient has:
- An
EmailAddress. - A display name.
- A data model instance (
TModel) that will be injected into the template for that specific recipient.
using PosInformatique.Foundations.EmailAddresses;
invitationEmail.Recipients.Add(
EmailAddress.Parse("alice@example.com"),
"Alice",
new InvitationEmailTemplateModel
{
FirstName = "Alice",
InvitationLink = "https://myapp.com/invite?code=ABC123"
});
invitationEmail.Recipients.Add(
EmailAddress.Parse("bob@example.com"),
"Bob",
new InvitationEmailTemplateModel
{
FirstName = "Bob",
InvitationLink = "https://myapp.com/invite?code=XYZ789"
});
3. Send the email
Once the email and its recipients are configured, you ask the IEmailManager to send it:
var cancellationToken = CancellationToken.None;
await emailManager.SendAsync(invitationEmail, cancellationToken);
Under the hood:
IEmailManageriterates over all recipients.- For each recipient, it applies the associated model to the template’s subject and HTML body.
- It builds an
EmailMessagewith:- The configured sender (
EmailingOptions.SenderEmailAddress). - The recipient address and display name.
- The generated subject and HTML content.
- The configured sender (
- It calls
IEmailProvider.SendAsync(...)to actually send the message.
The provider implementation is responsible for the technical details (SMTP, Azure Communication Service, etc.).
Summary
The typical flow is:
- Configure emailing:
- Register templates and sender via
AddEmailing(...). - Register an
IEmailProviderimplementation.
- Register templates and sender via
- Define and centralize template identifiers using
EmailTemplateIdentifier<TModel>. - Define data models by creating custom data class.
- At runtime, use
IEmailManager.Create(...)to instantiate a strongly-typed email. - Add recipients and models through
EmailRecipientCollection<TModel>. - Call
IEmailManager.SendAsync()to generate and send emails through theIEmailProvider.
Links
| Product | Versions 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 is compatible. 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. |
-
net8.0
- Microsoft.Extensions.Options (>= 8.0.0)
- PosInformatique.Foundations.EmailAddresses (>= 1.0.0)
- PosInformatique.Foundations.Text.Templating (>= 1.0.0)
-
net9.0
- Microsoft.Extensions.Options (>= 9.0.0)
- PosInformatique.Foundations.EmailAddresses (>= 1.0.0)
- PosInformatique.Foundations.Text.Templating (>= 1.0.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on PosInformatique.Foundations.Emailing:
| Package | Downloads |
|---|---|
|
PosInformatique.Foundations.Emailing.Azure
Provides an IEmailProvider implementation for PosInformatique.Foundations.Emailing using Azure Communication Service. Uses Azure.Communication.Email.EmailClient to send templated emails and can be configured via AddEmailing().UseAzureCommunicationService(), including EmailClient and EmailClientOptions customization. |
|
|
PosInformatique.Foundations.Emailing.Templates.Razor
Provides helpers to create EmailTemplate instances using Razor components for subject and HTML body. Built on top of PosInformatique.Foundations.Text.Templating.Razor, it supports strongly-typed models and Razor layout features for reusable email designs. |
|
|
PosInformatique.Foundations.Emailing.Graph
Provides an IEmailProvider implementation for PosInformatique.Foundations.Emailing using Microsoft Graph API. Uses Microsoft.Graph.GraphServiceClient to send templated emails through Microsoft 365 mailboxes with Azure AD authentication via TokenCredential. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.0-rc.2 | 56 | 1/26/2026 |
| 1.1.0-rc.1 | 69 | 1/23/2026 |
| 1.0.0 | 520 | 11/19/2025 |
| 1.0.0-rc.4 | 379 | 11/19/2025 |
| 1.0.0-rc.3 | 379 | 11/18/2025 |
| 1.0.0-rc.2 | 371 | 11/18/2025 |
| 1.0.0-rc.1 | 378 | 11/18/2025 |
1.0.0
- Initial release with Emailing infrastructure.