Click or drag to resize
MimeKit

BodyBuilder Class

A message body builder.
Inheritance Hierarchy
SystemObject
  MimeKitBodyBuilder

Namespace: MimeKit
Assembly: MimeKit (in MimeKit.dll) Version: 4.3.0
Syntax
C#
public class BodyBuilder

The BodyBuilder type exposes the following members.

Constructors
 NameDescription
Public methodCode exampleBodyBuilder Initialize a new instance of the BodyBuilder class.
Top
Properties
 NameDescription
Public propertyCode exampleAttachments Get the attachments.
Public propertyCode exampleHtmlBody Get or set the html body.
Public propertyCode exampleLinkedResources Get the linked resources.
Public propertyCode exampleTextBody Get or set the text body.
Top
Methods
 NameDescription
Public methodEquals
(Inherited from Object)
Protected methodFinalize
(Inherited from Object)
Public methodGetHashCode
(Inherited from Object)
Public methodGetType
(Inherited from Object)
Protected methodMemberwiseClone
(Inherited from Object)
Public methodCode exampleToMessageBody Construct the message body based on the text-based bodies, the linked resources, and the attachments.
Public methodToString
(Inherited from Object)
Top
Remarks
BodyBuilder is a helper class for building common MIME body structures.
Example
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?";

            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 ();
See Also