Skip to main content

Sending Emails with SMTP in C# / .NET

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.

Prerequisites

Before you begin, ensure you have:
  • .NET SDK installed (.NET 6.0 or higher recommended)
  • Visual Studio or Visual Studio Code for development
  • NuGet package manager access
  • Basic knowledge of C# programming
  • Domain verified in your AhaSend account
  • SMTP credentials created (username and password)
  • Access to your AhaSend dashboard for credential management
  • MailKit NuGet package (recommended)
  • MimeKit (included with MailKit)
  • Avoid using System.Net.Mail for PLAIN AUTH over STARTTLS
Need SMTP Credentials? If you haven’t created SMTP credentials yet, check out our SMTP Credentials guide for step-by-step instructions.

Why MailKit Over System.Net.Mail?

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

Connection Settings

Use these settings for all C#/.NET SMTP configurations with AhaSend:

Primary Server

Host: send.ahasend.com
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required

US Server

Host: send-us.ahasend.com
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required

❌ Why System.Net.Mail Fails

The built-in System.Net.Mail.SmtpClient in .NET has fundamental issues with STARTTLS authentication that make it incompatible with AhaSend.

The Problem

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.

What Doesn’t Work

Here’s the problematic code that fails with AhaSend:
System.Net.Mail (Don't Use This)

Debugging the Issue

You can confirm this authentication failure by capturing SMTP traffic with tools like tcpdump or Wireshark:
  1. Connection succeeds - Client connects to AhaSend SMTP server
  2. STARTTLS works - TLS encryption is established successfully
  3. AUTH command missing - Client never sends authentication credentials
  4. 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.

Installation

Install MailKit via NuGet Package Manager:
MimeKit Included: MailKit automatically includes MimeKit for MIME message construction. No additional packages needed!

Basic MailKit Examples

Advanced MailKit Examples

Testing with Sandbox Mode

Use sandbox mode to safely test your C# email integration:
Sandbox Benefits: Emails sent in sandbox mode are free, trigger webhooks normally, and never actually deliver to recipients - perfect for development and testing.

Resources

MailKit Documentation

Official MailKit and MimeKit documentation

Microsoft .NET Docs

Comprehensive .NET development guide

MailKit GitHub

Source code, examples, and issue tracking

AhaSend Support

Get help from our engineering team
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.