MSG extract attachments

Extracts and saves all attachments from the specified .msg message.

Usage

=====================================================================
ExtractAttachments.exe
=====================================================================

Extracts all attachments from a .msg e-mail message.
Supported e-mail formats: .msg (Microsoft Outlook).

The program is a sample for Rebex MSG for .NET component.
For more info, see https://www.rebex.net/msg/

Syntax is: MsgExtractAttachments.exe <mailfile.msg>

More info

The sample demonstrates:

  • Loading MsgMessage from a local file.
  • Iterating through attachment collection.
  • Saving attachments to a local file.

The following code snippet is the core of this sample.

C#

// Load mail message from disk
var mail = new MsgMessage();
mail.Load(args[0]);

Console.WriteLine(
    "Message contains {0} attachments.",
    mail.Attachments.Count
);

// If message has no attachments, just exit
if (mail.Attachments.Count == 0)
    return;

foreach (MsgAttachment attachment in mail.Attachments)
{
    // Print attachment's file name and Content-Type
    Console.WriteLine(
        "Saving '{0}' ({1}).",
        attachment.FileName,
        attachment.Properties.GetValue(tag: MsgPropertyTag.AttachMimeTag,
                                       defaultValue: MediaTypeNames.Application.Octet)
    );

    // Save the attachment to file
    attachment.Save(attachment.FileName);
}

VB.NET

' Load mail message from disk
Dim mail As New MsgMessage
mail.Load(args(0))

Console.WriteLine(
    "Message contains {0} attachments.",
    mail.Attachments.Count
)

' If message has no attachments, just exit
If mail.Attachments.Count = 0 Then Return

For Each attachment As MsgAttachment In mail.Attachments
    ' Print attachment's file name and Content-Type
    Console.WriteLine(
        "Saving '{0}' ({1}).",
        attachment.FileName,
        attachment.Properties.GetValue(tag:=MsgPropertyTag.AttachMimeTag,
                                       defaultValue:=MediaTypeNames.Application.Octet)
    )

    ' Save the attachment to file
    attachment.Save(attachment.FileName)
Next attachment