Messages and attachments

edit

Messages

A MapiMessage gives the common properties of an email: its Subject, its sender, its dates (in UTC), its Flags, its headers (TransportMessageHeaders) and its Recipients:

using var file = PstFile.Open(path);
var message = file.GetFolder("Inbox")!.EnumerateItems().First();
Assert.Equal("Welcome to Microsoft Office Outlook 2003", message.Subject);
Assert.Equal("Outlook 2003 Team", message.SenderName);
Assert.Single(message.Recipients);
Assert.HasCount(11, message.Attachments);
Assert.Contains("Outlook", message.Body, StringComparison.Ordinal);

An email has up to three bodies:

  • Body, the plain text,
  • BodyHtml, decoded with the code page of the message,
  • GetRtfBody, the RTF, decompressed from the compressed RTF property.

Bodies encapsulated in RTF

Outlook often keeps the HTML of a message only inside its RTF body, and the RTF of a message written from plain text says so (MS-OXRTFEX). GetHtmlBody returns the HTML property, or the HTML encapsulated in the RTF when the message has no HTML property, and GetTextBody does the same for the plain text. EmailMessage.FromMapi uses them, so that converting such a message to an .eml file keeps its HTML.

RtfEncapsulation works on the RTF itself: GetFormat reads the header, Decapsulate extracts the HTML or the text, and EncapsulateHtml and EncapsulateText write a body that SetRtfBody stores in an item:

var rtf = RtfEncapsulation.EncapsulateHtml("<p>Hello {world}</p>\r\n");
Assert.Equal(RtfEncapsulationFormat.Html, RtfEncapsulation.GetFormat(rtf));
Assert.Equal("<p>Hello {world}</p>\r\n", RtfEncapsulation.Decapsulate(rtf));

// An RTF body that does not encapsulate another format
Assert.Equal(RtfEncapsulationFormat.None, RtfEncapsulation.GetFormat(@"{\rtf1\ansi Hello}"u8));
Assert.Null(RtfEncapsulation.Decapsulate(@"{\rtf1\ansi Hello}"u8));

The reader follows the code pages of the header and of the fonts. The RTF that EncapsulateHtml writes contains only the HTML: a reader that does not support the encapsulation shows an empty document.

Attachments

Attachments lists the attachments. OpenRead reads the content block by block, without loading it in memory, and GetContent returns it as bytes:

using var file = PstFile.Open(path);
var message = file.GetFolder("Inbox")!.EnumerateItems().First();
foreach (var attachment in message.Attachments)
{
    if (attachment.Method == MapiAttachmentMethod.ByValue)
    {
        // Read the content block by block, without loading it in memory
        using var content = attachment.OpenRead();
        using var output = File.Create(Path.Combine(directory.FullName, Path.GetFileName(attachment.Name ?? "attachment")));
        content.CopyTo(output);
    }
}

An attachment whose Method is EmbeddedMessage contains a message: GetEmbeddedMessage returns it, with its own recipients and attachments. The exceptions of a recurring appointment are embedded messages too.

Associated items

EnumerateAssociatedItems enumerates the hidden items of a folder, where Outlook stores views, rules and settings.

Search folders

The items of a search folder (IsSearchFolder) are the results of its search, stored in other folders. Outlook stores the results of some search folders in a structure that [MS-PST] does not document: these search folders have no items.