MimeMessageTextBody Property

Get the text body of the message if it exists.

Definition

Namespace: MimeKit
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public string? TextBody { get; }

Property Value

String
The text body if it exists; otherwise, .

Implements

IMimeMessageTextBody

Remarks

Gets the text content of the first text/plain body part that is found (in depth-first search order) which is not an attachment.

Example

C#
                    public static string GetMessageText (MimeMessage message)
{
    // The TextBody and HtmlBody convenience properties find the text/plain and text/html
    // bodies of the message, respectively, taking multipart/alternative and multipart/related
    // structures into consideration. They return null if no suitable body exists.
    if (message.TextBody != null)
        return message.TextBody;

    if (message.HtmlBody != null) {
        // The message only has an HTML body. Convert it to plain text by stripping
        // out the HTML markup.
        var previewer = new HtmlTextPreviewer { MaximumPreviewLength = int.MaxValue };

        return previewer.GetPreviewText (message.HtmlBody);
    }

    // GetTextBody() can be used to look for other text formats, such as text/enriched.
    return message.GetTextBody (TextFormat.Enriched) ?? string.Empty;
}
                  

See Also