Send email from .NET over SMTP

System.Net.Mail.SmtpClient is obsolete. Use MailKit, STARTTLS on 587, username `unitpost`, API key as password.

What are the SMTP settings?

Hostsmtp.unitpost.com
Usernameunitpost(always this literal value)
PasswordYour Unitpost API key(must carry emails:send)
Port (STARTTLS)587(or 2587 if blocked)
Port (implicit TLS)465(or 2465 if blocked)
From addressAny address on a verified domain

How do I set it up?

  1. Add MailKit

    Shell
    dotnet add package MailKit
  2. Send

    C#
    using MailKit.Net.Smtp;
    using MailKit.Security;
    using MimeKit;
    
    var message = new MimeMessage();
    message.From.Add(MailboxAddress.Parse("you@yourdomain.com"));
    message.To.Add(MailboxAddress.Parse("customer@example.com"));
    message.Subject = "Hello from .NET";
    message.Body = new TextPart("html") { Text = "<p>Sent via SMTP.</p>" };
    
    using var client = new SmtpClient();
    await client.ConnectAsync("smtp.unitpost.com", 587, SecureSocketOptions.StartTls);
    await client.AuthenticateAsync("unitpost", Environment.GetEnvironmentVariable("UNITPOST_API_KEY"));
    await client.SendAsync(message);
    await client.DisconnectAsync(true);
  3. Verify delivery

    Confirm in Activity.