MailKit for C# (.NET): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

MailKit for C# (.NET): Complete Guide with Examples

\n\n

1. What It Is and Why You Need It

\n\n

MailKit is a powerful, cross-platform open-source library for working with email in the .NET ecosystem. It provides a full set of tools for creating, sending, receiving, and processing email messages. Unlike the outdated System.Net.Mail, MailKit offers a modern API, full support for MIME (Multipurpose Internet Mail Extensions), work with S/MIME and OpenPGP, as well as asynchronous operations, which is critically important for high-load applications.

\n\n

Why do you need MailKit? If you are developing in C# and need to send emails with attachments, parse incoming messages, work with different email protocols (SMTP, POP3, IMAP), or handle complex message formats (HTML, attachments, signatures), MailKit will become your indispensable tool. It is used in production environments by thousands of companies, including Microsoft (in some internal services), and supports .NET Framework, .NET Core, .NET 5+, and even Xamarin/MAUI.

\n\n

Key advantages of MailKit over alternatives: full support for the latest email standards (RFC 2045-2049, RFC 2822, RFC 5322), built-in SSL/TLS support, proxy support, streaming processing of large attachments, and high performance thanks to the use of System.IO.Pipelines and async/await.

\n\n

2. Installation

\n\n

MailKit is installed via the NuGet package manager. Run one of the following commands in the Package Manager Console or in the .NET CLI terminal:

\n\n
// Via .NET CLI\ndotnet add package MailKit\n\n// Via Package Manager Console\nInstall-Package MailKit\n\n// Or via NuGet Package Manager in Visual Studio\n// Find "MailKit" and install the latest stable version
\n\n

After installation, a dependency on MimeKit (the base library for working with MIME messages) will be added to the project — it is installed automatically as a transitive dependency.

\n\n

3. Quick Start — Minimal Working Example

\n\n

Below is a minimal example of sending a simple text email via the Gmail SMTP server. Note: to work with Gmail, you need to use an App Password or OAuth 2.0.

\n\n
using MailKit.Net.Smtp;\nusing MimeKit;\n\n// Create the message\nvar message = new MimeMessage();\nmessage.From.Add(new MailboxAddress("Sender", "sender@gmail.com"));\nmessage.To.Add(new MailboxAddress("Recipient", "recipient@example.com"));\nmessage.Subject = "Test email from MailKit";\nmessage.Body = new TextPart("plain") { Text = "Hello! This is a test message." };\n\n// Send via SMTP\nusing (var client = new SmtpClient())\n{\n    client.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);\n    client.Authenticate("sender@gmail.com", "your-app-password");\n    client.Send(message);\n    client.Disconnect(true);\n}\n\nConsole.WriteLine("Email sent!");
\n\n

4. Main Methods/Functions/Classes

\n\n

4.1. SmtpClient.Connect()

\n

Signature: Task ConnectAsync(string host, int port, SecureSocketOptions options, CancellationToken cancellationToken = default)

\n

What it does: Establishes a connection to the SMTP server. The SecureSocketOptions parameter determines the encryption type: None, Auto, SslOnConnect, StartTls.

\n\n
using var client = new SmtpClient();\nawait client.ConnectAsync("smtp.mail.ru", 465, SecureSocketOptions.SslOnConnect);
\n\n

4.2. SmtpClient.Authenticate()

\n

Signature: Task AuthenticateAsync(string user

Recommendations