Sending Email in C# (.NET): From SmtpClient to MailKit
Sending emails is one of the key tasks when developing web applications, notification services, and enterprise systems. The .NET ecosystem offers both built-in tools and third-party libraries that simplify working with SMTP, SSL/TLS, and complex scenarios. In this article, we will thoroughly examine how to send an email in C# using the classic SmtpClient and the modern MailKit library.
1. Built-in SmtpClient (System.Net.Mail)
The SmtpClient class is available in .NET Framework and .NET Core/5+ (with some limitations). It allows you to quickly configure sending an email through any SMTP server.
Basic Email Sending Example
using System.Net;using System.Net.Mail;
var smtpClient = new SmtpClient("smtp.gmail.com"){ Port = 587, Credentials = new NetworkCredential("your_email@gmail.com", "your_app_password"), EnableSsl = true};
var mailMessage = new MailMessage{ From = new MailAddress("your_email@gmail.com"), Subject = "Test email from C#", Body = "Hello! This message was sent via SmtpClient.", IsBodyHtml = false};
mailMessage.To.Add("recipient@example.com");
try{ smtpClient.Send(mailMessage); Console.WriteLine("Email sent successfully!");}catch (Exception ex){ Console.WriteLine($"Sending error: {ex.Message}");}Important: For Gmail, you will need an app password, not your regular password. For other services (Yandex, Mail.ru), check their SMTP settings.
Sending an HTML Email with an Attachment
var message = new MailMessage();message.From = new MailAddress("sender@example.com");message.To.Add("user@example.com");message.Subject = "Monthly Report";message.IsBodyHtml = true;message.Body = "<h2>Monthly Report</h2><p>A PDF file is attached.</p>";
// Add an attachmentvar attachment = new Attachment(@"C:\\Reports\\report.pdf");message.Attachments.Add(attachment);
using (var client = new SmtpClient("smtp.example.com", 587)){ client.Credentials = new NetworkCredential("login", "password"); client.EnableSsl = true; client.Send(message);}Attachments can be of any type: images, documents, archives. To send from memory, use MemoryStream.
2. Asynchronous Sending
In modern applications, it is advisable not to block the thread. Use SendMailAsync:
public async Task SendEmailAsync(){ using var client = new SmtpClient("smtp.example.com", 587); client.Credentials = new NetworkCredential("login", "password"); client.EnableSsl = true;
var message = new MailMessage("from@example.com", "to@example.com") { Subject = "Asynchronous Sending", Body = "This email was sent asynchronously." };
await client.SendMailAsync(message); Console.WriteLine("Sent asynchronously!");}3. Problems with SmtpClient and Alternatives
Despite its simplicity, SmtpClient has drawbacks:
- Outdated API (marked as obsolete in .NET 5+);
- Poor support for modern authentication protocols (OAuth2);
- Lack of built-in support for S/MIME and advanced scenarios.
Therefore, Microsoft recommends using third-party libraries such as MailKit.
4. MailKit — A Modern Library for Working with Email
MailKit is a cross-platform open-source library. It supports SMTP, POP3, IMAP, SSL/TLS, asynchrony, OAuth2, and much more.