Creating messages |
MimeKit provides a number of ways to create messages.
This topic contains the following sections:
Creating MIME messages using MimeKit is really trivial.
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?"; message.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 " };
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. Some other examples include enriched, rtf, and xml.
The Text property is the easiest way to both get and set the string content of the MIME part.
Attachments are just like any other MimePart, the only difference is that they typically have a Content-Disposition header with a value of attachment instead of inline or no Content-Disposition header at all.
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;
Of course, that is just a simple example. A lot of modern mail clients such as Outlook or Thunderbird will send out both a text/html and a text/plain version of the message text. To do this, you'd create a TextPart for each part and then add them to a multipart/alternative like so:
var attachment = CreateImageAttachment (); var plain = CreateTextPlainPart (); var html = CreateTextHtmlPart (); // Note: it is important that the text/html part is added second, because it is the // most expressive version and (probably) the most faithful to the sender's WYSIWYG // editor. var alternative = new MultipartAlternative (); alternative.Add (plain); alternative.Add (html); // now create the multipart/mixed container to hold the multipart/alternative // and the image attachment var multipart = new Multipart ("mixed"); multipart.Add (alternative); multipart.Add (attachment); // now set the multipart/mixed as the message body message.Body = multipart;
If you are used to System.Net.Mail's API for creating messages, you will probably find using a BodyBuilder much more friendly than manually creating the tree of MIME parts. Here's how you could create a message body using a BodyBuilder:
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?"; var builder = new BodyBuilder (); // Set the plain-text version of the message text builder.TextBody = @"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 "; // In order to reference selfie.jpg from the html text, we'll need to add it // to builder.LinkedResources and then use its Content-Id value in the img src. var image = builder.LinkedResources.Add (@"C:\Users\Joey\Documents\Selfies\selfie.jpg"); image.ContentId = MimeUtils.GenerateMessageId (); // Set the html version of the message text builder.HtmlBody = string.Format (@"<p>Hey Alice,<br> <p>What are you up to this weekend? Monica is throwing one of her parties on Saturday and I was hoping you could make it.<br> <p>Will you be my +1?<br> <p>-- Joey<br> <center><img src=""cid:{0}""></center>", image.ContentId); // We may also want to attach a calendar event for Monica's party... builder.Attachments.Add (@"C:\Users\Joey\Documents\party.ics"); // Now we just need to set the message body and we're done message.Body = builder.ToMessageBody ();
To make things even simpler still, MimeKit allows you to explicitly cast a MailMessage to a MimeMessage.