Search code examples
c#outlookvstooutlook-addinoffice-addins

Outlook get attachement binary return NULL : 0x37010102 / PR_ATTACH_DATA_BIN | VSTO adding c#


I run code from a while now and it always work fine on every PC, but recently a client get an error.

When I run the same code on that specific client who run everything exactly like the other client configuration i receive a NULL byte[] array. Did could be a setting in Microsoft Outlook ?

    public const string PR_ATTACH_DATA_BIN = "http://schemas.microsoft.com/mapi/proptag/0x37010102";
    Attachment attachment;
    Microsoft.Office.Interop.Outlook.PropertyAccessor pacc = attachment.PropertyAccessor;
    byte[] filebyte = (byte[])pacc.GetProperty(PR_ATTACH_DATA_BIN);
    Convert.ToBase64String(filebyte);

The binary return converted in base64 is ... AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=

In fact it's a null if i convert that to a string. The attachement is supposed to be UTF8 text file.

What did i miss ?


Solution

  • Firstly, you need to check Attachment.Type to make sure it is olAttachByValue - PR_ATTACH_DATA_BIN won't be present for other attachment types, such as message attachments or embedded OLE objects.

    Secondly, PropertyAccessor.GetProperty (which uses IMAPIProp::GetProps under the hood) won't return large binary or string properties: on the MAPI level, you need to open the property (IMAPIProp::OpenProperty) as IStream.

    In this particular case, your only workaround is saving the attachment as file (Attachment.SaveAsFile). If using Redemption is an option (I am its author), you can use RDOAttachment.AsArray / AsString / AsStream properties. Its Fields[] indexed property can also return large binary and string properties.