Tutorial

Send Email with .NET (C#) via SMTP

Use the built-in System.Net.Mail client, or MailKit when you want modern TLS, async and reliable error reporting.

By the Sendcorex team·Updated Sep 21, 2026·9 min read

Option 1: System.Net.Mail.SmtpClient

Built in, but marked obsolete for new development — use port 587 with EnableSsl = true:

using System.Net;
using System.Net.Mail;

var apiKey = Environment.GetEnvironmentVariable("SENDCOREX_API_KEY")!;

using var client = new SmtpClient("smtp.sendcorex.com", 587)
{
    EnableSsl = true,
    Credentials = new NetworkCredential(apiKey, apiKey),
};

using var message = new MailMessage
{
    From = new MailAddress("hello@yourdomain.com", "Your App"),
    Subject = "Welcome!",
    Body = "<h1>You are in.</h1>",
    IsBodyHtml = true,
};
message.To.Add("customer@example.com");

await client.SendMailAsync(message);

Option 2: MailKit (recommended)

dotnet add package MailKit
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;

var apiKey = Environment.GetEnvironmentVariable("SENDCOREX_API_KEY")!;

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Your App", "hello@yourdomain.com"));
message.To.Add(new MailboxAddress("", "customer@example.com"));
message.Subject = "Welcome!";
message.Body = new TextPart("html") { Text = "<h1>You are in.</h1>" };

using var smtp = new SmtpClient();
await smtp.ConnectAsync("smtp.sendcorex.com", 587, SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync(apiKey, apiKey);
await smtp.SendAsync(message);
await smtp.DisconnectAsync(true);

Which should you use?

Use MailKit for all new code — it has correct TLS handling on every port, full async with cancellation, and is actively maintained. Keep SmtpClient only in existing code you don't want to touch.

Troubleshooting

SymptomFix
Authentication failedConfirm the env var loaded and is used as both username and password.
SmtpProtocolExceptionPort 465 needs SslOnConnect, not StartTls.

Send your first email in minutes

Create a free account, verify your domain and paste in the code above.

No credit card required