Send emails through AhaSend’s SMTP servers using C# and .NET with the powerful MailKit library. This guide covers why you should avoid System.Net.Mail and use MailKit instead for reliable email delivery.
Avoid System.Net.Mail: The built-in SmtpClient in .NET does not reliably support AUTH PLAIN over STARTTLS, causing authentication failures with AhaSend. Use MailKit instead for guaranteed compatibility.
AhaSend works with any SMTP-compatible library, but for C#/.NET applications, we strongly recommend MailKit over the built-in System.Net.Mail:
✅ MailKit (Recommended)
Pros: Modern, reliable STARTTLS Auth: AUTH PLAIN/LOGIN support Compatibility: Works with StartTLS Features: Rich MIME support, attachments, HTML
❌ System.Net.Mail (Avoid)
Issues: Unreliable STARTTLS authentication Auth: Silent AUTH failures with AhaSend Status: Legacy, minimal updates Problems: May not send AUTH commands at all
Even with correct credentials and EnableSsl = true, the SMTP client often never issues an AUTH command after the STARTTLS handshake. This causes silent authentication failures where the connection appears to succeed but emails are rejected.
Tested and Confirmed: We’ve tested this issue on .NET 9 across Linux and Windows. The problem persists across different .NET versions and platforms.
You can confirm this authentication failure by capturing SMTP traffic with tools like tcpdump or Wireshark:
Connection succeeds - Client connects to AhaSend SMTP server
STARTTLS works - TLS encryption is established successfully
AUTH command missing - Client never sends authentication credentials
Silent failure - No clear error message, just rejection
Know a Workaround? If you’ve found a way to make System.Net.Mail work reliably with AhaSend’s AUTH PLAIN over STARTTLS, please contact our support team. We’d be happy to update this guide with your solution!
MailKit is a modern, actively maintained .NET library that correctly handles SMTP authentication with AhaSend. It properly negotiates authentication mechanisms like PLAIN and LOGIN over STARTTLS.
Use sandbox mode to safely test your C# email integration:
Copy
using System;using MailKit.Net.Smtp;using MailKit.Security;using MimeKit;class Program{ static void Main() { using (var client = new SmtpClient()) { try { // 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 sandbox test message var message = new MimeMessage(); message.From.Add(new MailboxAddress("Test", "[email protected]")); message.To.Add(new MailboxAddress("Test User", "[email protected]")); message.Subject = "Sandbox Test Email"; // Enable sandbox mode message.Headers.Add("AhaSend-Sandbox", "true"); message.Headers.Add("AhaSend-Sandbox-Result", "deliver"); message.Body = new TextPart("plain") { Text = "This email is sent in sandbox mode for testing." }; client.Send(message); client.Disconnect(true); Console.WriteLine("Sandbox email sent successfully!"); } catch (Exception ex) { Console.WriteLine($"Failed to send sandbox email: {ex.Message}"); } } }}
Sandbox Benefits: Emails sent in sandbox mode are free, trigger webhooks normally, and never actually deliver to recipients - perfect for development and testing.
Pro Tip: Always use MailKit instead of System.Net.Mail for AhaSend integration. Configure your SMTP settings through dependency injection, implement proper error handling with retry logic, and start with sandbox mode during development. Use async patterns for better performance in web applications.