Discuss this help topic in SecureBlackbox Forum

MIME: Extract attachments

Although attached data can be located on any level of the message part hierarchy, they all are available as an array, accessible using TElMessage.AttachmentCount and TElMessage.Attachments[] properties (note that in some languages Attachments property will be accessible via GetAttachments() or get_Attachments() method).

The following code snippet enumerates all attachments available in a message and shows their file names and sizes in bytes:

C#:


for (int i = 0; i < message.AttachmentsCount; i++)
{
    TElMessagePart attachment = message.get_Attachments(i);
    Console.WriteLine("{0} = {1} bytes", attachment.FileName, attachment.DataSize);
}

The following code snippet saves the attachment data to the file:

C#:


void ExtractAttachments(TElMessage message, string destFolder)
{
    if (!Directory.Exists(destFolder))
        Directory.CreateDirectory(destFolder);

    for (int i = 0; i < message.AttachmentsCount; i++)
    {
        TElMessagePart attachment = message.get_Attachments(i);

        string fileName = Path.Combine(destFolder, attachment.FileName);

        byte[] data = null;
        int dataSize = 0;

        int err = attachment.GetData(ref data, ref dataSize);

        if (err == 0)
            File.WriteAllBytes(fileName, data);
        else
            Console.WriteLine("Failed to get data from attachment {0}", i);
    }
}

How To articles about MIME

Discuss this help topic in SecureBlackbox Forum