MapiAttachment Class

An attachment of an item: a file, or an embedded message.

public sealed class MapiAttachment

Example

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);
    }
}

Properties

NameDescription
ContentIdGets the content identifier, which the HTML body uses to show the attachment inline (cid: URLs).
DisplayNameGets the display name of the attachment, the name Outlook shows (for an embedded message, its subject).
ExtensionGets the extension of the file name, such as .pdf.
FileNameGets the short (8.3) file name of the attachment.
IsEmbeddedMessageGets whether the attachment is an embedded message.
IsHiddenGets whether the attachment is hidden, such as an image shown inline in the HTML body.
LongFileNameGets the full file name of the attachment.
MessageGets the message the attachment belongs to.
MethodGets how the content of the attachment is stored.
MimeTypeGets the MIME type of the content, such as image/png.
NameGets the best available name: the long file name, the short file name, or the display name.
PropertiesGets the properties of the attachment.
RenderingPositionGets the position of the attachment in the rich text body, or -1.
SizeGets the size of the attachment, as stored in the file (it includes the size of the properties).

Gets the content identifier, which the HTML body uses to show the attachment inline (cid: URLs).

public string? ContentId { get; }

Property value

string?

Gets the display name of the attachment, the name Outlook shows (for an embedded message, its subject).

public string? DisplayName { get; }

Property value

string?

Gets the extension of the file name, such as .pdf.

public string? Extension { get; }

Property value

string?

Gets the short (8.3) file name of the attachment.

public string? FileName { get; }

Property value

string?

IsEmbeddedMessage

MapiAttachment.cs

Gets whether the attachment is an embedded message.

public bool IsEmbeddedMessage { get; }

Property value

bool

Gets whether the attachment is hidden, such as an image shown inline in the HTML body.

public bool IsHidden { get; }

Property value

bool

LongFileName

MapiAttachment.cs

Gets the full file name of the attachment.

public string? LongFileName { get; }

Property value

string?

Gets the message the attachment belongs to.

public MapiMessage Message { get; }

Property value

MapiMessage

Gets how the content of the attachment is stored.

public MapiAttachmentMethod Method { get; }

Property value

MapiAttachmentMethod

Gets the MIME type of the content, such as image/png.

public string? MimeType { get; }

Property value

string?

Gets the best available name: the long file name, the short file name, or the display name.

public string? Name { get; }

Property value

string?

Gets the properties of the attachment.

public MapiPropertyCollection Properties { get; }

RenderingPosition

MapiAttachment.cs

Gets the position of the attachment in the rich text body, or -1.

public int RenderingPosition { get; }

Property value

int

Gets the size of the attachment, as stored in the file (it includes the size of the properties).

public int? Size { get; }

Property value

int?

Methods

NameDescription
GetContent()Returns the content of the attachment.
GetEmbeddedMessage()Returns the embedded message of an attachment whose method is MapiAttachmentMethod.EmbeddedMessage.
OpenRead()Opens a stream on the content of the attachment. Large contents are read block by block.
ToString()Returns the name of the attachment.

GetContent()

MapiAttachment.cs

Returns the content of the attachment.

public byte[] GetContent()

Returns

byte[] The content; an empty array when the attachment has no binary content.

Exceptions

InvalidEmailDataException
The content is malformed.
EmailResourceLimitException
The content is larger than PstReadOptions.MaxPropertySize.

GetEmbeddedMessage()

MapiAttachment.cs

Returns the embedded message of an attachment whose method is MapiAttachmentMethod.EmbeddedMessage.

public MapiMessage? GetEmbeddedMessage()

Returns

MapiMessage? The embedded message, or null when the attachment is not an embedded message.

Exceptions

InvalidEmailDataException
The embedded message is malformed.
EmailResourceLimitException
The embedded messages are nested deeper than PstReadOptions.MaxEmbeddedMessageDepth.

Opens a stream on the content of the attachment. Large contents are read block by block.

public Stream OpenRead()

Returns

Stream A read-only stream; an empty stream when the attachment has no binary content (such as an embedded message).

Exceptions

InvalidEmailDataException
The content is malformed.

Returns the name of the attachment.

public override string ToString()

Returns

string The name of the attachment.