A simple .NET 8.0 client for sending email via Mailgun’s HTTP API.
- .NET 8.0 / C# latest
- No dependencies: .NET only, no external library dependencies
- Built-in config validation (
MailgunClientConfig
) - Supports:
To
,Cc
,Bcc
,From
,Subject
,Text
&HTML
bodies, tags, tracking, scheduled delivery
- Mailgun account & API key
- Verified sending domain in Mailgun (Mailgun provides DNS record instructions when you set up a Sending Domain)
dotnet add package DdotM.EmailClient.Mailgun
Or via Package Manager:
Install-Package DdotM.EmailClient.Mailgun
- Sign in to Mailgun and go to the Sending section in your dashboard.
- Add or verify your sending domain (e.g.
mg.yourdomain.com
). - Navigate to the API Keys or Security tab and create a new API key.
- Save your API key in a secure location (environment variable, secrets store, etc.)—you’ll need it in your application.
After adding your sending domain in the Mailgun dashboard, Mailgun will display a set of DNS records (SPF, DKIM, DMARC, tracking CNAME, etc.).
- Log in to your DNS provider (where you registered your domain).
- Create or update the records exactly as Mailgun specifies for your sending domain (e.g.
mg.yourdomain.com
). - Save your changes and wait for propagation (usually < 60 minutes).
- Verify using a DNS lookup tool (
dig
,nslookup
, or MXToolbox) that each record resolves correctly.
using DdotM.EmailClient.Mailgun;
// 1. Configure
var config = new MailgunClientConfig
{
ApiKey = "YOUR_MAILGUN_API_KEY",
SendingDomain = "mg.yourdomain.com"
};
// 2. Create client
var mailer = new MailgunClient(config);
// 3. Build message
var msg = new MailgunMessage
{
From = new Recipient { Name = "Alice", Address = "alice@yourdomain.com" },
Subject = "Hello from Mailgun!",
TextBody = "This is a plain-text body.",
HtmlBody = "<p>This is an HTML body.</p>",
Tracking = true,
DeliveryTime = DateTime.UtcNow.AddMinutes(10)
};
// 4. Add recipients & tags
msg.ToEmails.Add(new Recipient { Name = "Bob", Address = "bob@example.com" });
msg.CcEmails.Add(new Recipient { Name = "Eve", Address = "eve@example.org" });
msg.Tags.Add("welcome-email");
// 5. Send
MailgunMessage result = await mailer.SendAsync(msg);
Console.WriteLine($"Status: {result.Response.StatusCode}");