views:

777

answers:

2

I am using Microsoft.Office.Interop.Outlook in C# to access a mailbox and find messages with specific file attachments. I need to interact with the attachments.

Currently, I am saving the file to a new location and accessing it from there. This copying process is slowing me down. I'd ideally like to access the file from wherever it is being stored in memory, but the Outlook.Attachment.PathName field is returning null.

The messages are being stored on an Exchange server.

Is it possible to access them directly or do I need to continue saving the files?

A: 

Outlook.Attachment.PathName implies a disk location. The attachment is a file; if you want to interact with it, you have to treat it like a file, and that means saving it out to the local disk.

The Outlook.Attachment object does not actually contain the attached file; it contains a link to the attached file, with some associated metadata.

Robert Harvey
The attachment isn't neccessarily stored on disk, e.g. for Exchange messages.
Georg Fritzsche
A: 

I don't know if its possible in any way via the Outlook Object Model, but MAPI surely allows you that.

You need to open the messages MAPIObject, an IMessage. From there use IMessage::OpenAttach(), which returns an IAttach.
The attachment size is stored in PR_ATTACH_SIZE, the actual data in PR_ATTACH_DATA_BIN.

As always, Redemption probably has an easier way of doing this.

edit:
Access via MAPI is what Outlook does internally anyway - should be reasonably fast, at least when using Exchange Cached Mode.

Georg Fritzsche