Search code examples
c#outlookinteropoffice-interopcom-interop

Microsoft Interop OutLook C#- Cannot save attachments of type OLE


I have a C# program saving attachments from unread emails from Outlook mail box to a folder and the below line of code breaks(first line) for attachment types that are of OLE type with the error "Outlook cannot perform this action on this type of attachment"(Where 'it' is of type MailItem).

string attachedfilename = it.Attachments[i].FileName;
it.Attachments[i].SaveAsFile("C:\\temp\\"+attachedfilename);

I have read articles on using http://www.dimastr.com/redemption/RDOMail.htm (library) to overcome this problem for Ole type attachments, but apart from this option can I make use of any other .NET Library to overcome this problem?
If yes, kindly share the code snippet in C#.


Solution

  • You would need to call IAttach::OpenProperty(PR_ATTACH_DATA_OBJ, IID_IStorage, ...) then open a particular stream from IStorage that contains the data that you are after. Note that the stream and its format are specific to the application that created the OLE attachment. IAttach::OpenProperty is only available in Extended MAPI (C++ or Delphi), you cannot do that in C#. Redemption (I am its author) supports Word Pad, Paint Brush, Excel, Power Point, Word, Open Office, Acrobat, bitmap, metafile, etc. formats when you call RDOAttachment.SaveAsFile.

    To see the data stored in Outlook, take a look at the attachment with OutlookSpy (I am also its author): select the message, click IMessage button, go to the GetAttachmentTable tab, double click on the attachment, right click on the PR_ATTACH_DATA_OBJ property, select IMAPIProp::OpenProperty, select IStorage interface.