.NET

MailKit against a sandbox — connect, authenticate, send.

Before you start

  • .NET 6 or later
  • MailKit

1Add MailKit

dotnet add package MailKit

2Send a test message

Your sandbox credentials
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;

var message = new MimeMessage();
message.From.Add(MailboxAddress.Parse("staging@example.com"));
message.To.Add(MailboxAddress.Parse("anyone@example.com"));
message.Subject = "Sandbox check";
message.Body = new TextPart("html") { Text = "<h1>Hello from .NET</h1>" };

using var client = new SmtpClient();
await client.ConnectAsync("smtp.mailclutch.com", 587, SecureSocketOptions.StartTls);
await client.AuthenticateAsync("mc_0000000000000000", "pw_0000000000000000");
await client.SendAsync(message);
await client.DisconnectAsync(true);

3Or configure ASP.NET

If your app reads its mail settings from configuration, the same values go in appsettings.Development.json:

appsettings.Development.json
{
  "Smtp": {
    "Host": "smtp.mailclutch.com",
    "Port": 587,
    "Username": "mc_0000000000000000",
    "Password": "pw_0000000000000000"
  }
}

Watch it land

Open the sandbox and the message is there — rendered HTML, plain text, raw source, attachments, and the SPF, DKIM and DMARC verdicts. It updates live, so you can leave it open next to your terminal.

Nothing is ever delivered onward, whatever address you send to; claim the inbox with an account and it stays.

Awaiting your email

Send from any address; it stops here rather than going onward. The inbox keeps what it catches for 24 hours.

What usually goes wrong

  • System.Net.Mail.SmtpClient

    The built-in client still works — new SmtpClient("smtp.mailclutch.com", 587) { Credentials = new NetworkCredential(user, pass), EnableSsl = true } — but Microsoft marks it obsolete for new code, which is why MailKit is shown first.

  • SecureSocketOptions

    StartTls for 587, SslOnConnect for 465. Auto also works; None will fail authentication.

Official documentationMailKit (opens in a new tab)