MapiAttachment Class
An attachment of an item: a file, or an embedded message.
public sealed class MapiAttachmentExample
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
| Name | Description |
|---|---|
| ContentId | Gets the content identifier, which the HTML body uses to show the attachment inline (cid: URLs). |
| DisplayName | Gets the display name of the attachment, the name Outlook shows (for an embedded message, its subject). |
| Extension | Gets the extension of the file name, such as .pdf. |
| FileName | Gets the short (8.3) file name of the attachment. |
| IsEmbeddedMessage | Gets whether the attachment is an embedded message. |
| IsHidden | Gets whether the attachment is hidden, such as an image shown inline in the HTML body. |
| LongFileName | Gets the full file name of the attachment. |
| Message | Gets the message the attachment belongs to. |
| Method | Gets how the content of the attachment is stored. |
| MimeType | Gets the MIME type of the content, such as image/png. |
| Name | Gets the best available name: the long file name, the short file name, or the display name. |
| Properties | Gets the properties of the attachment. |
| RenderingPosition | Gets the position of the attachment in the rich text body, or -1. |
| Size | Gets the size of the attachment, as stored in the file (it includes the size of the properties). |
ContentId
MapiAttachment.csDisplayName
MapiAttachment.csExtension
MapiAttachment.csFileName
MapiAttachment.csIsEmbeddedMessage
MapiAttachment.csIsHidden
MapiAttachment.csLongFileName
MapiAttachment.csMessage
MapiAttachment.csMethod
MapiAttachment.csGets how the content of the attachment is stored.
public MapiAttachmentMethod Method { get; }Property value
MimeType
MapiAttachment.csProperties
MapiAttachment.csGets the properties of the attachment.
public MapiPropertyCollection Properties { get; }Property value
RenderingPosition
MapiAttachment.csMethods
| Name | Description |
|---|---|
| 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.csReturns 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.csReturns 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.
OpenRead()
MapiAttachment.csOpens 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.