Guide · .NET & Elixir
How to send email with C# and .NET
Send email from C# and ASP.NET Core with HttpClient and the Stack Emails API, or with MailKit over SMTP.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
export STACKEMAILS_API_KEY=se_live_xxxxxxxxxxxx
2. Send your first email
using System.Net.Http.Headers;
using System.Net.Http.Json;
var http = new HttpClient();
http.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("STACKEMAILS_API_KEY"));
var res = await http.PostAsJsonAsync("https://stackemails.com/v1/emails", new
{
from = "Acme <hello@mail.acme.com>",
to = "ada@example.com",
subject = "Welcome to Acme",
html = "<p>Thanks for signing up!</p>",
});
Console.WriteLine($"{(int)res.StatusCode} {await res.Content.ReadAsStringAsync()}");
A successful request returns 202 with the message id, for example { "id": "msg_5b1f0c6c2a8e4e0f", "status": "queued" }. Errors return a JSON body with error.code and error.message (see errors).
3. Or send over SMTP with MailKit
// dotnet add package MailKit
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Acme", "hello@mail.acme.com"));
message.To.Add(MailboxAddress.Parse("ada@example.com"));
message.Subject = "Welcome to Acme";
message.Body = new TextPart("html") { Text = "<p>Thanks for signing up!</p>" };
using var client = new SmtpClient();
await client.ConnectAsync("smtp.stackemails.com", 465, SecureSocketOptions.SslOnConnect);
await client.AuthenticateAsync("stackemails", Environment.GetEnvironmentVariable("STACKEMAILS_API_KEY"));
await client.SendAsync(message);
await client.DisconnectAsync(true);
In ASP.NET Core, register a typed client with builder.Services.AddHttpClient<EmailService>() instead of creating HttpClient per request. Microsoft recommends MailKit over the legacy System.Net.Mail.SmtpClient.
Production checklist
- Keep the API key in an environment variable or secret manager, never in client-side code.
- Add an
Idempotency-Keyheader to retries so a timeout never sends twice (idempotency). - Subscribe to webhooks for bounces and complaints.
- Check your domain's SPF, DKIM and DMARC records.
- Start from a tested email template for password resets, receipts and invites.