Class MapiAttachment

MapiAttachment class

Represents an attachment of an Outlook item.

public class MapiAttachment : MapiPropertyContainer

Properties

NameDescription
BinaryData { get; set; }Gets or sets the content of a file attachment.
virtual CodePage { get; }Gets the code page of the ANSI strings of the object.
Content { get; }Gets the content of the attachment.
ContentLength { get; }Gets the length of the attachment content in bytes.
DisplayName { get; set; }Gets or sets the name of the attachment shown to the user.
Extension { get; }Gets the file name extension of the attachment, such as .docx.
FileName { get; }Gets the short (8.3) file name of the attachment, without a path. Use LongFileName for the full name.
virtual IsInline { get; }Gets a value indicating whether the attachment is shown inside the message body (an inline image, for example) rather than as a regular attachment.
IsReference { get; }Gets a value indicating whether the attachment is stored as a reference rather than being embedded in the email.
virtual ItemId { get; }Gets the identifier of the attachment on the mail server, for attachments obtained from a server; null otherwise.
LongFileName { get; }Gets the full file name of the attachment, without a path.
MimeTag { get; }Gets the MIME content type of the attachment, such as image/png.
NamedProperties { get; }Gets the named properties of the attachment.
ObjectData { get; }Gets the embedded object of the attachment, such as an attached Outlook message or an OLE document.
virtual Properties { get; }Gets all properties of the object.
PropertyStream { get; }Gets the property stream of the attachment.
SubStorages { get; }Gets the sub-storages of the attachment.

Methods

NameDescription
static LoadFromTnef(Stream)Loads an attachment from a stream in TNEF format.
static LoadFromTnef(string)Loads an attachment from a file in TNEF format.
override GetProperty(PropertyDescriptor)Gets a MAPI property of the attachment by its descriptor.
GetPropertyBoolean(long)Gets the value of a property as a Boolean.
GetPropertyBytes(long)Gets the value of a property as bytes.
GetPropertyDateTime(long)Gets the value of a property as a date and time.
GetPropertyInt32(long)Gets the value of a property as a 32-bit integer.
GetPropertyLong(long)Gets the value of a property as a 64-bit integer.
GetPropertyShort(long)Gets the value of a property as a 16-bit integer.
GetPropertyString(long)Gets the value of a property as a string.
GetPropertyString(long, int)Gets the value of a property as a string, decoding an ANSI string with the specified code page.
IsStoreUnicodeOk()Determines whether the string properties of the object are stored in Unicode.
OpenRead()Opens the attachment content for reading.
RemoveProperty(long)Removes a property from the attachment, including its named-property mapping.
Save(Stream)Saves the content of the attachment to a stream. An embedded message is saved in MSG format.
Save(string)Saves the content of the attachment to a file. An embedded message is saved in MSG format.
SaveAsync(Stream, CancellationToken)Save attachment content asynchronously.
SaveAsync(string, CancellationToken)Save attachment content asynchronously.
SaveToTnef(Stream)Saves the attachment to a stream in TNEF format.
SaveToTnef(string)Saves the attachment to a file in TNEF format.
SaveToTnefAsync(Stream, CancellationToken)Save attachment to tnef content asynchronously.
SaveToTnefAsync(string, CancellationToken)Save attachment to tnef content asynchronously.
override SetProperty(MapiProperty)Adds the property to the attachment, or replaces the property with the same tag.
override SetProperty(PropertyDescriptor, object)Sets the value of a MAPI property of the attachment.
TryGetPropertyData(long)Gets the value of a property as bytes.
TryGetPropertyDateTime(long, ref DateTime)Gets the value of a property as a date and time, if the property exists.
TryGetPropertyInt32(long, ref int)Gets the value of a property as a 32-bit integer, if the property exists.
TryGetPropertyLong(long, ref long)Gets the value of a property as a 64-bit integer, if the property exists.
TryGetPropertyString(long)Gets the value of a property as a string.
TryGetPropertyString(long, int)Gets the value of a property as a string, decoding an ANSI string with the specified code page.
TryGetPropertyString(long, ref string)Gets the value of a property as a string, if the property exists.
TryGetPropertyString(long, ref string, int)Gets the value of a property as a string, if the property exists, decoding an ANSI string with the specified code page.

Remarks

An attachment is either a file, whose content is in BinaryData, or an embedded object, such as an attached Outlook message, which is in ObjectData. Like the message itself, the attachment is a set of MAPI properties: use the inherited property methods for data that has no dedicated member.

Add attachments through the MapiAttachmentCollection returned by Attachments.

Examples

This example saves the attachments of a message: an embedded message is loaded as a MapiMessage, any other attachment is saved to a file.

[C#]

var msg = MapiMessage.Load("source.msg");

foreach (MapiAttachment attachment in msg.Attachments)
{
    if (attachment.IsInline)
    {
        // an image or object shown in the message body
        Console.WriteLine($"Inline: {attachment.DisplayName}");
    }
    else if (attachment.ObjectData != null && attachment.ObjectData.IsOutlookMessage)
    {
        // an attached Outlook message
        MapiMessage embedded = attachment.ObjectData.ToMapiMessage();
        Console.WriteLine($"Embedded message: {embedded.Subject}");
    }
    else
    {
        attachment.Save(Path.Combine("output", attachment.LongFileName));
    }
}

[Visual Basic]

Dim msg = MapiMessage.Load("source.msg")

For Each attachment As MapiAttachment In msg.Attachments
    If attachment.IsInline Then
        ' an image or object shown in the message body
        Console.WriteLine($"Inline: {attachment.DisplayName}")
    ElseIf attachment.ObjectData IsNot Nothing AndAlso attachment.ObjectData.IsOutlookMessage Then
        ' an attached Outlook message
        Dim embedded As MapiMessage = attachment.ObjectData.ToMapiMessage()
        Console.WriteLine($"Embedded message: {embedded.Subject}")
    Else
        attachment.Save(Path.Combine("output", attachment.LongFileName))
    End If
Next

See Also