1. Products
  2.   Email
  3.   .NET
  4.   MailKit
 
  

Open Source .NET Library for IMAP, POP3 & SMTP

Free C# .NET Library for Generating Message with Attachments, Encrypt/Decrypt Messages with PGP/MIME. 

MailKit is an Open Source .NET library for IMAP, POP3, and SMTP. It is a cross-platform mail client library built on top of MimeKit. The project aims to provide a robust, fully-featured, and RFC-compliant SMTP, POP3, and IMAP client implementations.

The API supports several important features related to SASL Authentication, proxy support, SMTP client, POP3 client, IMAP4 client, client-side sorting, and threading of messages.

Previous Next

Getting Started with MailKit

The easiest way to install MailKit is via NuGet. To use it from Visual Studio’s Package Manager Console, please enter the following command.

install Mailkit via NuGet

Install-Package MailKit  

Install Mailkit via GitHub 

git clone --recursive https://github.com/jstedfast/MailKit.git 

Create New Messages via .NET

The Open Source API MailKit library enables software developers to create MIME messages with a few simple commands. A TextPart is a leaf-node MIME part with a text media type. The first argument to the TextPart constructor specifies the media-subtype, in this case, plain. Another media subtype you are probably familiar with is the HTML subtype. The easiest way to get and set both the string content of the MIME part is the Text property.

The Open Source API MailKit library enables software developers to create MIME messages with few simple commands. A TextPart is a leaf-node MIME part with a text media-type. The first argument to the TextPart constructor specifies the media-subtype, in this case, plain. Another media subtype you are probably familiar with is the HTML subtype. The easiest way for both get and set the string content of the MIME part is the Text property.

Generate and Send Message Free using C#

var message = new MimeMessage();
message.From.Add(new MailboxAddress("fred", "This email address is being protected from spam-bots. You need JavaScript enabled to view it."));
message.To.Add(new MailboxAddress("frans", "This email address is being protected from spam-bots. You need JavaScript enabled to view it."));
message.Subject = "FileFormat ";

message.Body = new TextPart("plain")
{
  Text = "File Format Developer Guide"
};

using (var client = new SmtpClient())
{
  // For demo-purposes,
  client.ServerCertificateValidationCallback = (s, c, h, e) => true;

  client.Connect("smtp.test.com", 587, false);

  // Note: only needed if the SMTP server requires authentication
  client.Authenticate("frans", "password");

  client.Send(message);
  client.Disconnect(true);
}                  

Generate Message with Attachments using .NET API

MailKit API provides features for generating a message with attachments inside .NET applications. Attachments are just like any other MimePart; the main difference is that they contain a content-disposition header holding value of the attachment instead of inline or no Content-Disposition header at all. To send out both a text/HTML and a text/plain version of the message, you need to create a TextPart for each part and then add them to a multipart/alternative.

Create Message with Attachments via C#


var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
message.Subject = "How you doin?";

// create our message text, just like before (except don't set it as the message.Body)
var body = new TextPart ("plain") {
  Text = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
"
};

// create an image attachment for the file located at path
var attachment = new MimePart ("image", "gif") {
  Content = new MimeContent (File.OpenRead (path), ContentEncoding.Default),
  ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
  ContentTransferEncoding = ContentEncoding.Base64,
  FileName = Path.GetFileName (path)
};

// now create the multipart/mixed container to hold the message text and the
// image attachment
var multipart = new Multipart ("mixed");
multipart.Add (body);
multipart.Add (attachment);

// now set the multipart/mixed as the message body
message.Body = multipart;
 

Encrypt/Decrypt Messages with PGP/MIME

The MailKit library provides features for encrypting email messages with PGP/MIME inside .NET applications. The PGP/MIME uses a MIME part with a multipart/encrypted mime-type to encapsulate encrypted data. If you want to encrypt a message, it is always a better approach to use SecureMailboxAddress instead of a MailboxAddress for every recipient, which will allow users to specify the unique fingerprint of each recipient's PGP key.

Encrypting Messages with PGP/MIME via C#


public void Encrypt (MimeMessage message)
{
  // encrypt our message body using our custom GnuPG cryptography context
  using (var ctx = new MyGnuPGContext ()) {
    // Note: this assumes that each of the recipients has a PGP key associated
    // with their email address in the user's public keyring.
    // 
    // If this is not the case, you can use SecureMailboxAddresses instead of
    // normal MailboxAddresses which would allow you to specify the fingerprint
    // of their PGP keys. You could also choose to use one of the Encrypt()
    // overloads that take a list of PgpPublicKeys.
    message.Body = MultipartEncrypted.Encrypt (ctx, message.To.Mailboxes, message.Body);
  }
}

Decrypting PGP/MIME Messages


public MimeEntity Decrypt (MimeMessage message)
{
  if (message.Body is MultipartEncrypted) {
    // the top-level MIME part of the message is encrypted using PGP/MIME
    var encrypted = (MultipartEncrypted) entity;

    return encrypted.Decrypt ();
  } else {
    // the top-level MIME part is not encrypted
    return message.Body;
  }
}
 English