Search code examples
c#office365outlook-addinmapioutlook-2016

Attachment.PropertyAccessor.GetProperty for PR_ATTACH_DATA_BIN returns byte array with all items 0


I have an Outlook add-in created for sending large file attachments. When a user tries to attach a file it actually attaches a placeholder ".txt" file whose contents are the path to the actual attachment. When the user sends the email the add-in tries to read the contents of the placeholder attachment in order to get the path to the actual attachment. It does this using the Attachment.PropertyAccessor.GetProperty method while passing in the value "http://schemas.microsoft.com/mapi/proptag/0x37010102" (PR_ATTACH_DATA_BIN).

byte[] attachmentData = attachment.PropertyAccessor.GetProperty(
  "http://schemas.microsoft.com/mapi/proptag/0x37010102");

This has been working for several years and I haven't updated my add-in for 4 months but it has been acting differently starting today. Now the return value for this method returns a byte array of the correct length (in my case 93 for the 93-byte placeholder file) but all of the items in the byte array are 0. If I open the placeholder file all of the contents look correct so I don't understand the issue. We are currently on the latest build of Outlook 2016 (Version 1711 Build 8730.2127) and I've even tried reverting to the last 2 builds but the problem still occurs.

Does anyone know if something has changed with Outlook 2016 regarding the MAPI?

Note - I've tried using OutlookSpy to find out what's going on but the GetAttachmentTable tab shows no entries even though from the Outlook UI I can clearly see that there is an attachment.


Solution

  • I was able to fix this somehow by saving the new email after adding my attachment but before sending.

    mailItem.Attachments.Add(filePath, OlAttachmentType.olByValue, Type.Missing, Type.Missing);
    mailItem.Save();
    

    I tried this after reading the description for the Attachments.Add method which stated in the remarks:

    When an Attachment is added to the Attachments collection of an item, the Type property of the Attachment will always return olOLE (6) until the item is saved. To ensure consistent results, always save an item before adding or removing objects in the Attachments collection.

    I tried saving before adding the attachment but that had no effect. That led me to try saving after adding and when I did that I was able to get the correct value out of the "PR_ATTACH_DATA_BIN" property.