MailKit - Working with Emails

July 21, 2023 by Khalid Abuhakmeh

Email is to the internet like fish are to water, it's an essential part of the ecosystem, and we couldn't imagine one without the other. While its ubiquity is sometimes taken for granted and thought of as the medium you get meeting invites and funny meme's from your relatives, the varying protocols that emails rely on can be highly nuanced. Luckily for the .NET community, Jeffrey Stedfast and his .NET Foundation project MailKit have spent thousands of hours working to produce the best email library.

What is MailKit?

As you've likely realized, MailKit is a cross-platform mail client supporting SMTP, POP3, and IMAP4 protocols. It also supports various authentication methods such as NTLM, LOGIN, OAUTHBEARER, and more. For folks working within secure environments, MailKit also has proxy support to get emails through firewalls. Each part of MailKit was laboriously built from the ground up to adhere to the specifications of each protocol while considering the potential inconsistencies of vendor implementations to provide for a smoother development experience. If you need to send or receive emails, MailKit is your choice.

Let's see how to get started with MailKit and what the code may look like in your .NET applications.

Getting Started with MailKit

MailKit works in any .NET application on any platform. For this introduction, let's use a Console application. Start by adding the .NET package to your existing application.

dotnet add package MailKit

Next, we'll email a fictitious friend about a fictitious event. Change the code to a real SMTP server to get a working sample.

using MailKit.Net.Smtp;  
using MimeKit;  

var message = new MimeMessage();  
message.From.Add(new MailboxAddress("Joey Tribbiani", "[email protected]"));  
message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "[email protected]"));  
message.Subject = "How you doin'?";  

message.Body = new TextPart("plain")  
{  
    Text = """  
    Hey Chandler,  

    Monica and I were going to play some paintball, you in?  

    -- Joey  
    """  
};  

using var client = new SmtpClient();  
client.Connect("smtp.friends.com", 587, false);  
// Note: only needed if the SMTP server requires authentication  
client.Authenticate("joey", "password");  
client.Send(message);  
client.Disconnect(true);

You can also use MailKit to retrieve messages from an existing POP3 server.

using MailKit.Net.Pop3;  

using var client = new Pop3Client();  
client.Connect("pop.friends.com", 110, false);  
client.Authenticate("joey", "password");  

for (var i = 0; i < client.Count; i++)  
{  
var message = client.GetMessage(i);  
Console.WriteLine("Subject: {0}", message.Subject);  
}  

client.Disconnect(true);

As noted before, IMAP servers are also supported, with many newer email servers preferring the IMAP protocol; this is a must-have feature of any email library.

using MailKit;  
using MailKit.Net.Imap;  

using var client = new ImapClient();  
client.Connect("imap.friends.com", 993, true);  

client.Authenticate("joey", "password");  

// The Inbox folder is always available on all IMAP servers...  
var inbox = client.Inbox;  
inbox.Open(FolderAccess.ReadOnly);  

Console.WriteLine("Total messages: {0}", inbox.Count);  
Console.WriteLine("Recent messages: {0}", inbox.Recent);  

for (var i = 0; i < inbox.Count; i++)  
{  
var message = inbox.GetMessage(i);  
Console.WriteLine("Subject: {0}", message.Subject);  
}  

client.Disconnect(true);

These three use cases are likely what most developers are looking for when dealing with an email server, and MailKit provides them in a few lines of code. That's amazing.

Conclusion

MailKit boasts over 77.8 million downloads on NuGet with no signs of slowing down anytime soon. Additionally, contributors continue to keep the project up to date with commits as recent as a month ago. If you need a library that can send and receive emails all from the comfort of your favorite platform, .NET, you can't go wrong by choosing MailKit.

If you want to leap from a project consumer to a contributor, head over to the GitHub repository and start a discussion with the community. Also, the project has provided a lot of value to the community, so if you're in a position to do so, please consider donating directly to MailKit. Any contributions are always welcome.

Finally, the .NET Foundation is a volunteer organization, so if you have any time or effort you'd like to donate to spotlight your favorite project, please feel free to contact us.

image credit: Brett Jordan