Cédric Boivin
Cédric Boivin

Reputation: 11351

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 ?

Upvotes: 1

Views: 362

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66255

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.

Upvotes: 1

Eugene Astafiev
Eugene Astafiev

Reputation: 49405

The Outlook object model applies its own business restrictions to the methods and properties. The PropertyAccessor.GetProperty method can't be used for reading large properties. For example, for binary properties only those whose values are under 4,088 byte can be retrieved or set. If trying to use larger values, you may get an out-of-memory error. You can read more about limitations in the OOM in the article I wrote for the technical blog a long time ago - Don't stumble over a stone working with the PropertyAccessor and StorageItem classes in Outlook 2007.

So, you want to continue using the binary data without saving attached files on the disk you may consider using a low-level API on which Outlook is based on - Extended MAPI. Or just any third-party wrapper around that API such as Redemption.

But the simplest way is to save attached files on the disk by using the Attachment.SaveAsFile method and then read the content back if required.

Upvotes: 1

Related Questions