Sending Email in C# (.NET): SmtpClient, MailKit, and MimeKit
Sending email is one of the basic tasks in modern applications. In the C# (.NET) ecosystem, there are both built-in tools (the SmtpClient class) and powerful third-party libraries (MailKit and MimeKit). In this guide, we will thoroughly cover both approaches, review code examples, SMTP server configuration, working with attachments, and HTML emails.
1. Built-in SmtpClient (System.Net.Mail)
The SmtpClient class is available in .NET Framework and .NET Core/5+ (with limitations). It is simple to use but does not support modern protocols (e.g., OAuth2) and is considered deprecated for new projects.
2. Installing and Connecting MailKit
MailKit is a cross-platform library for working with email. It is fully asynchronous, supports SSL/TLS, OAuth2, S/MIME, and other modern features. Installation via NuGet:
dotnet add package MailKitdotnet add package MimeKitOr via the Package Manager Console:
Install-Package MailKitInstall-Package MimeKit3. Example of sending a simple email using SmtpClient
Basic example using the built-in SmtpClient:
using System.Net;using System.Net.Mail;
public void SendSimpleEmail(){ var fromAddress = new MailAddress("sender@example.com", "Sender"); var toAddress = new MailAddress("recipient@example.com", "Recipient"); const string fromPassword = "your_password"; const string subject = "Email Subject"; const string body = "Email Body";
var smtp = new SmtpClient { Host = "smtp.example.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) };
using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body }) { smtp.Send(message); }}Important: For Gmail, Yandex, and other services, you need to use an app password or OAuth2.
4. Sending Email using MailKit (recommended method)
MailKit provides a more flexible and modern API. Example of sending a simple text email:
using MailKit.Net.Smtp;using MailKit.Security;using MimeKit;using System.Threading.Tasks;
public async Task SendEmailWithMailKitAsync(){ var message = new MimeMessage(); message.From.Add(new MailboxAddress("Sender", "sender@example.com")); message.To.Add(new MailboxAddress("Recipient", "recipient@example.com")); message.Subject = "Email Subject"; message.Body = new TextPart("plain") { Text = "Email Body" };
using (var client = new SmtpClient()) { await client.ConnectAsync("smtp.example.com", 587, SecureSocketOptions.StartTls); await client.AuthenticateAsync("sender@example.com", "your_password"); await client.SendAsync(message); await client.DisconnectAsync(true); }}5. Sending an HTML email with attachments
Creating a multipart email (HTML + attachments) using MimeKit:
public async Task SendHtmlEmailWithAttachmentAsync(){ var message = new MimeMessage(); message.From.Add(new MailboxAddress("Sender", "sender@example.com")); message.To.Add(new MailboxAddress("Recipient", "recipient@example.com")); message.Subject = "HTML email with attachment";
var bodyBuilder = new BodyBuilder { HtmlBody = @"<h1>Hello!</h1><p>This is an HTML email with an attachment.</p>", TextBody = "This is a text