PosInformatique.Foundations.Emailing.Graph
provides an IEmailProvider
implementation for PosInformatique.Foundations.Emailing based on the Microsoft Graph API.
It uses Microsoft.Graph.GraphServiceClient
to send templated emails (created via IEmailManager)
through a Microsoft 365 mailbox, using Azure AD authentication.
Authentication is fully driven by an Azure.Core.TokenCredential instance, allowing you to use:
- Managed identity
- Client credentials (client id/secret or certificate)
- Interactive login, device code, etc.
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.Emailing.GraphIEmailProviderimplementation usingMicrosoft.Graph.GraphServiceClient.- Simple configuration through
AddEmailing().UseGraph(...). - Authentication configured via
TokenCredential:DefaultAzureCredential(managed identity, VS, CLI, etc.)ClientSecretCredential,ClientCertificateCredential, etc.
- Optional
baseUrlparameter to customize the Graph endpoint (defaults tohttps://graph.microsoft.com/v1.0). - Sends HTML emails using the
EmailMessageproduced by PosInformatique.Foundations.Emailing.
using Azure.Identity;
using Microsoft.Extensions.DependencyInjection;
using PosInformatique.Foundations.EmailAddresses;
using PosInformatique.Foundations.Emailing;
using PosInformatique.Foundations.Emailing.Graph;
var services = new ServiceCollection();
// TokenCredential for Microsoft Graph (for example: managed identity or local dev)
var credential = new DefaultAzureCredential();
services
.AddEmailing(options =>
{
options.SenderEmailAddress = EmailAddress.Parse("sender@yourtenant.onmicrosoft.com");
// Register your templates here...
// options.RegisterTemplate(EmailTemplateIdentifiers.Invitation, invitationTemplate);
})
.UseGraph(credential);If you want to authenticate with a client id / tenant id / client secret:
using Azure.Identity;
using PosInformatique.Foundations.Emailing.Graph;
var tenantId = configuration["AzureAd:TenantId"];
var clientId = configuration["AzureAd:ClientId"];
var clientSecret = configuration["AzureAd:ClientSecret"];
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
services
.AddEmailing(options =>
{
options.SenderEmailAddress = EmailAddress.Parse("sender@yourtenant.onmicrosoft.com");
})
.UseGraph(credential);The TokenCredential you provide is responsible for acquiring tokens for the Microsoft Graph API. The provider does not manage scopes or credentials itself; this is entirely delegated to the credential implementation.
You can optionally customize the Graph base URL (for example, for national clouds):
var baseUrl = "https://graph.microsoft.com/v1.0/beta";
services
.AddEmailing(options =>
{
options.SenderEmailAddress = EmailAddress.Parse("sender@yourtenant.onmicrosoft.com");
})
.UseGraph(credential, baseUrl);If baseUrl is null, https://graph.microsoft.com/v1.0 is used by default.
- Configure emailing (sender address, templates) with
AddEmailing(...). - Configure the Graph provider with
UseGraph(TokenCredential, baseUrl?). - Inject
IEmailManagerand create emails from template identifiers. - Add recipients and models.
- Call
SendAsync(...)to send emails via Microsoft Graph.