user1677408
user1677408

Reputation: 41

How to access attachment data programmatically in outlook if attachment is itself a mail

I am programmatically trying to get the attachment data in C# in following way :--

Microsoft.Office.Interop.Outlook.Attachment attachment = objMail.Attachments[attachmentIndex];

if (attachment.DisplayName.Equals("Test"))

{ 

   const string PR_ATTACH_DATA = "http://schemas.microsoft.com/mapi/proptag/0x37010102";

    byte[] attachmentData = attachment.PropertyAccessor.GetProperty(PR_ATTACH_DATA);

}

Now my code is working fine if attachment is text file or image file. But if attachment is itself a mail, it throws the exception that property is unknown or can not be found.

Please suggest in which cases / type of attachments, this property "http://schemas.microsoft.com/mapi/proptag/0x37010102" will not work and in those cases, what would be alternative property / method to get the attachment data in byte array ?

Thanks

Upvotes: 1

Views: 1422

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66316

PR_ATTACH_DATA_BIN is only present for the regular by-value attachments (PR_ATTACH_METHOD == ATTACH_BY_VALUE). Embedded message or OLE attachments do not expose that property - they use PR_ATTACH_DATA_OBJ which must be opened using IAttach::OpenProperty(IID_IStorage, ...) - take a look at the existing messages using OutlookSpy - I am its author - select the message, click IMessage button, go to GetAttachmentTable tab, double click on the attachment.

Also keep in mind that PropertyAccessor.GetProperty would only be able to retrieve that property for small attachments. For large attachments, PR_ATTACH_DATA_BIN must be opened as IStream using IAttach::OpenProperty(IID_IStorage, ...) - PropertyAccessor.GetProperty does not do that. You will need to use Extended MAPI (C++ or Delphi) or Redemption (I am its author) - it exposes RDOAttachment.AsArray / AsText / AsStream properties.

Upvotes: 2

trm5073
trm5073

Reputation: 91

Microsoft Graph Rest API is an single end point and wrapper for most Microsoft Data including events, most office products including outlook. Best of all any language can make a request to the endpoint and retrieve the information. See the complete Docs HERE to get started.

See below code for a simple Get request for outlook attachments. Note there are other more complex implementations. Docs: https://learn.microsoft.com/en-us/graph/api/attachment-get?view=graph-rest-1.0&tabs=http Scroll through the link and you can find C#, Java, and JavaScript examples on how to implement this.

GET /me/messages/{id}/attachments/{id}
GET /users/{id | userPrincipalName}/messages/{id}/attachments/{id}

GET /me/messages/{id}/attachments/{id}/$value
GET /users/{id | userPrincipalName}/messages/{id}/attachments/{id}/$value

Upvotes: 0

Related Questions