Skip to content

Latest commit

 

History

History
104 lines (77 loc) · 2.68 KB

File metadata and controls

104 lines (77 loc) · 2.68 KB

Graph Email Sender

A minimal C# console app that sends email via the Microsoft Graph API using app-only authentication (client credentials / service-principal flow).


Prerequisites

Requirement Version
.NET SDK 8.0 +
Azure AD App Registration see below

1 — Register an Azure AD App

  1. Go to Azure Portal → Azure Active Directory → App registrations → New registration.
  2. Give it a name (e.g. GraphEmailSender) and click Register.
  3. Note down:
    • Application (client) ID
    • Directory (tenant) ID
  4. Under Certificates & secrets → New client secret — create a secret and copy the Value.
  5. Under API permissions → Add a permission → Microsoft Graph → Application permissions, add Mail.Send.
  6. Click Grant admin consent for your tenant.

⚠️ Mail.Send is an application permission (not delegated).
It lets the app send mail as any user in the tenant — restrict access via an application access policy if needed.


2 — Set Environment Variables

# Windows (PowerShell)
$env:AZURE_TENANT_ID     = "your-tenant-id"
$env:AZURE_CLIENT_ID     = "your-client-id"
$env:AZURE_CLIENT_SECRET = "your-client-secret"
$env:SENDER_EMAIL        = "sender@yourdomain.com"

# macOS / Linux
export AZURE_TENANT_ID="your-tenant-id"
export AZURE_CLIENT_ID="your-client-id"
export AZURE_CLIENT_SECRET="your-client-secret"
export SENDER_EMAIL="sender@yourdomain.com"

3 — Run the App

cd GraphEmailSender
dotnet run

Project Structure

GraphEmailSender/
├── GraphEmailSender.csproj   # NuGet references
├── Program.cs                # All app logic
└── README.md

Key classes in Program.cs

Class / Record Purpose
AppConfig Loads credentials from environment variables
GraphClientFactory Creates GraphServiceClient with ClientSecretCredential
EmailMessage Immutable record describing an outbound email
EmailSender Wraps the Graph sendMail API call

NuGet Packages

Package Why
Microsoft.Graph Typed Graph SDK (models + request builders)
Azure.Identity ClientSecretCredential for app-only auth

Customising the Email

Edit the email variable in Program.cs:

var email = new EmailMessage
{
    To      = ["alice@example.com", "bob@example.com"],
    Cc      = ["manager@example.com"],
    Subject = "Your subject here",
    Body    = "<p>HTML or plain text body</p>",
    IsHtml  = true,
};