Sending email with C# using STARTTLS and PLAIN AUTH


AhaSend works with any SMTP-compatible library — but if you're using C#, we strongly recommend using MailKit instead of the older System.Net.Mail API.

The built-in SmtpClient in .NET does not reliably support AUTH PLAIN over STARTTLS, even if you provide the correct credentials and enable TLS. This can cause authentication to silently fail, and AhaSend will reject the connection without issuing the AUTH command at all.

Don't use System.Net.Mail

We tested the following code using .NET 9 on Linux and Windows. Even with EnableSsl = true and correct credentials, the SMTP client never issues anAUTH command.

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

class Program
{
     static void Main(string[] args)
     {
         SmtpClient smtp = new SmtpClient("send.ahasend.com", 587);
         // Enable StartTLS
         smtp.EnableSsl = true;
         
         // Don't use the OS/Windows account. We want to supply our own credentials.
         smtp.UseDefaultCredentials = false; 
         smtp.Credentials = new NetworkCredential("YOUR_SMTP_USERNAME", "YOUR_SMTP_PASSWORD");

         MailMessage message = new MailMessage("[email protected]", "[email protected]");
         message.Subject = "Testing Auth";
         message.Body = "This is a test email via AUTH PLAIN.";

         try
         {
             smtp.Send(message);
             Console.WriteLine("Email sent successfully.");
         }
         catch (Exception ex)
         {
             Console.WriteLine("Error sending email: " + ex.Message);
             if (ex.InnerException != null)
             {
                 Console.WriteLine("Inner Exception: " + ex.InnerException.Message);
             }
         }
     }
}

You can confirm this by capturing the SMTP traffic with a tool like tcpdump or Wireshark — the client connects, performs STARTTLS, but never sends an AUTH command.

Please get in touch with us if you know of a workaround for getting around this limitation with built-in DotNet libraries. We'd be happy to update this guide to include your suggestions.

MailKit is a modern, actively maintained .NET library for working with email. It correctly negotiates authentication mechanisms like PLAIN and LOGIN over STARTTLS, and works perfectly with AhaSend.

Install MailKit via NuGet:

dotnet add package MailKit

Then send email like this:

using System;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;

class Program
{
     static void Main()
     {
         using (var client = new SmtpClient())
         {
             // Connect to AhaSend SMTP with STARTTLS
             client.Connect("send.ahasend.com", 587, SecureSocketOptions.StartTls);

             // Authenticate with your SMTP credentials
             client.Authenticate("YOUR_SMTP_USERNAME", "YOUR_SMTP_PASSWORD");

             // Create a simple message
             var message = new MimeMessage();
             message.From.Add(new MailboxAddress("Your Name", "[email protected]"));
             message.To.Add(new MailboxAddress("Recipient", "[email protected]"));
             message.Subject = "Hello from MailKit + AhaSend";
             message.Body = new TextPart("plain")
             {
                 Text = "This is a test email sent using MailKit and STARTTLS."
             };

             client.Send(message);
             client.Disconnect(true);
         }

         Console.WriteLine("Email sent successfully.");
     }
}

Summary

If you're using .NET, avoid System.Net.Mail.SmtpClient. It often fails to authenticate properly when using STARTTLS. Use MailKit instead. It's actively maintained, easy to use, and works out of the box with AhaSend.

Need help integrating it into your app? Contact us and we'll be happy to assist.

Category
AhaSend
Send up to 1,000 emails per month on us, no credit card required!