Click or drag to resize
MimeKit

BodyPartText Class

A textual body part.
Inheritance Hierarchy

Namespace:  MailKit
Assembly:  MailKit (in MailKit.dll) Version: 3.0.0
Syntax
C#
public class BodyPartText : BodyPartBasic

The BodyPartText type exposes the following members.

Constructors
  NameDescription
Public methodBodyPartText
Initializes a new instance of the BodyPartText class.
Top
Properties
  NameDescription
Public propertyContentDescription
Gets the Content-Description of the body part, if available.
(Inherited from BodyPartBasic.)
Public propertyContentDisposition
Gets the Content-Disposition of the body part, if available.
(Inherited from BodyPartBasic.)
Public propertyContentId
Gets the Content-Id of the body part, if available.
(Inherited from BodyPartBasic.)
Public propertyContentLanguage
Gets the Content-Language of the body part, if available.
(Inherited from BodyPartBasic.)
Public propertyContentLocation
Gets the Content-Location of the body part, if available.
(Inherited from BodyPartBasic.)
Public propertyContentMd5
Gets the MD5 hash of the content, if available.
(Inherited from BodyPartBasic.)
Public propertyContentTransferEncoding
Gets the Content-Transfer-Encoding of the body part.
(Inherited from BodyPartBasic.)
Public propertyContentType
Gets the Content-Type of the body part.
(Inherited from BodyPart.)
Public propertyFileName
Get the name of the file.
(Inherited from BodyPartBasic.)
Public propertyIsAttachment
Determines whether or not the body part is an attachment.
(Inherited from BodyPartBasic.)
Public propertyIsHtml
Gets whether or not this text part contains HTML.
Public propertyIsPlain
Gets whether or not this text part contains plain text.
Public propertyLines
Gets the length of the text, in lines.
Public propertyOctets
Gets the size of the body part, in bytes.
(Inherited from BodyPartBasic.)
Public propertyCode examplePartSpecifier
Gets the part specifier.
(Inherited from BodyPart.)
Top
Methods
  NameDescription
Public methodAccept
Dispatches to the specific visit method for this MIME body part.
(Overrides BodyPartBasicAccept(BodyPartVisitor).)
Protected methodEncode
Encodes the BodyPart into the StringBuilder.
(Overrides BodyPartBasicEncode(StringBuilder).)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a String that represents the current BodyPart.
(Inherited from BodyPart.)
Top
Remarks
Represents any body part with a media type of "text".
Examples
C#
public static void DownloadBodyParts ()
{
    using (var client = new ImapClient ()) {
        client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);

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

        client.Inbox.Open (FolderAccess.ReadOnly);

        // search for messages where the Subject header contains either "MimeKit" or "MailKit"
        var query = SearchQuery.SubjectContains ("MimeKit").Or (SearchQuery.SubjectContains ("MailKit"));
        var uids = client.Inbox.Search (query);

        // fetch summary information for the search results (we will want the UID and the BODYSTRUCTURE
        // of each message so that we can extract the text body and the attachments)
        var items = client.Inbox.Fetch (uids, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure);

        foreach (var item in items) {
            // determine a directory to save stuff in
            var directory = Path.Combine (baseDirectory, item.UniqueId.ToString ());

            // create the directory
            Directory.CreateDirectory (directory);

            // IMessageSummary.TextBody is a convenience property that finds the 'text/plain' body part for us
            var bodyPart = item.TextBody;

            // download the 'text/plain' body part
            var body = (TextPart) client.Inbox.GetBodyPart (item.UniqueId, bodyPart);

            // TextPart.Text is a convenience property that decodes the content and converts the result to
            // a string for us
            var text = body.Text;

            File.WriteAllText (Path.Combine (directory, "body.txt"), text);

            // now iterate over all of the attachments and save them to disk
            foreach (var attachment in item.Attachments) {
                // download the attachment just like we did with the body
                var entity = client.Inbox.GetBodyPart (item.UniqueId, attachment);

                // attachments can be either message/rfc822 parts or regular MIME parts
                if (entity is MessagePart) {
                    var rfc822 = (MessagePart) entity;

                    var path = Path.Combine (directory, attachment.PartSpecifier + ".eml");

                    rfc822.Message.WriteTo (path);
                } else {
                    var part = (MimePart) entity;

                    // note: it's possible for this to be null, but most will specify a filename
                    var fileName = part.FileName;

                    var path = Path.Combine (directory, fileName);

                    // decode and save the content to a file
                    using (var stream = File.Create (path))
                        part.Content.DecodeTo (stream);
                }
            }
        }

        client.Disconnect (true);
    }
}
See Also