CBHacking CBHacking - 3 months ago 68
C# Question

Get Outlook.MailItem from IMessage

I'm working on an Outlook add-in (VSTO, C#) that needs to be able to access the smime.p7m file from a signed or encrypted message. I know how to get the relevant

, but I'm hoping to do this in C# without needing to implement too many COM wrappers. Here's what I've got so far:

I wrote a C# wrapper for
IMAPISecureMessage
. It's not long or complex. I can take an
Outlook.MailItem
and get its
MAPIOBJECT
property, which appears to just be the underlying
IMessage
. I can then cast that
IMessage
to my
IMAPISecureMessage
wrapper and call
GetBaseMessage()
on it. This all appears to work.

The problem is, this returns an
IMessage
. I need to extract the attachment (smime.p7m) from this
IMessage
. I was hoping I could do that by wrapping a
MailItem
around the returned
IMessage
, but I can't find any way to do that. I can see how to create a
MailItem
(
Application.CreateItem
or
Items.Add
), but I can't set the
MAPIOBJECT
property of an existing
MailItem
, and I don't see a way to create a
MailItem
from an existing MAPI object (such as my
IMessage
from
GetBaseMessage()
).

Is there a way to do create the desired
MailItem
? If not, is there a way I can get the attachment (smime.p7m) out of the
IMessage
without either implementing a whole bunch of COM interfaces or using a proprietary third-party library (such as Redemption)?

Answer

You could of course create IMessage, IMAPITable, IAttach interface definitions and call IMessage::GetAttachmentTable, then IMAPITable::SetColumns/QueryRows (or HrQueryAllRows) to retrieve PR_ATTACH_NUM, then call IMessage::OpenAttach to open the attachment, then IAttach.OpenProperty(PR_ATTACH_DATA_BIN, IID_IStream, ...) to open the binary data as IStream.

You can also try to change the message class of the message to IPM.Note, save it, completely release (and pray Outlook does not keep a reference), then open it as a regular MailItem using Namespace.GetItemFromID.

Or, as you mentioned, you can use Redemption and its RDOEncryptedMessage object.

Comments