How to save encrypted mail to disk (MSG) unencrypted

  • Thread starter Thread starter Damian Dekoszczak
  • Start date Start date
D

Damian Dekoszczak

Hi

as in topic, i'm looking for a programmatic way to save encrypted
email from inbox as unencrypted MSG file on disk. I want to do it from
outlook add-in. Outlook Spy does it, so somehow it is possible. Of
course saving by means on MailItem::SaveAs saves encrypted mail and so
does code below:


void SaveToMSG ( MailItem _item)

{

stringfilePath ="C:\\Users\\user\\Desktop\\"+ _item.Subject + ".msg";

IntPtriSecMsg =IntPtr.Zero;

IntPtriMsg;

IntPtriProp;

IntPtrptrGuid;

variUnknown =ptrGuid =iMsg = iProp = IntPtr.Zero;

IMapiPropprop = null;



iUnknown= Marshal.GetIUnknownForObject(_item.MAPIOBJECT);

var secMsg= (IMAPISecureMessage)_item.MAPIOBJECT;

secMsg.GetBaseMessage(refiMsg);

Marshal.QueryInterface(iMsg, ref _propGuid, out iProp);

prop=
(IMapiProp)Marshal.GetTypedObjectForIUnknown(iProp,typeof(IMapiProp));



IStoragepStorage =null;

IntPtrpMsgSession = IntPtr.Zero;

IMessagepIMsg = null;

varpMalloc =MAPIGetDefaultMalloc();



StgCreateDocfile(filePath,

STGM.READWRITE |

STGM.TRANSACTED |

STGM.CREATE, 0, out pStorage);



OpenIMsgSession(pMalloc, 0, out pMsgSession);



OpenIMsgOnIStg(pMsgSession,

Marshal.GetFunctionPointerForDelegate(new
MAPIAllocateBufferDelegate(MAPIAllocateBuffer)),

Marshal.GetFunctionPointerForDelegate(new
MAPIAllocateMoreDelegate(MAPIAllocateMore)),

Marshal.GetFunctionPointerForDelegate(new
MAPIFreeBufferDelegate(MAPIFreeBuffer)),

pMalloc,

IntPtr.Zero,

pStorage,

IntPtr.Zero, 0, 0x80000000, outpIMsg);



r= WriteClassStg(pStorage, new Guid("{00020D0B-0000-0000-
C000-000000000046}") );



var guid = new Guid("00020307-0000-0000-C000-000000000046");

r = prop.CopyTo(0, null,IntPtr.Zero,IntPtr.Zero, IntPtr.Zero,guid,
pIMsg, 0,IntPtr.Zero);

pIMsg.SaveChanges ( 2 );

pStorage.Commit(0);



Marshal.FinalReleaseComObject(pIMsg);

Marshal.FinalReleaseComObject(pStorage);

Marshal.ReleaseComObject(prop);

CloseIMsgSession( pMsgSession );

}



Any hints how to do it? i tried passing IMAPISecureMessage IID as a
second parameter of IMAPIProp::CopyTo (rgiidExclude) but to no avail.
 
Back
Top