How get content from Outlook.MailItem attachment

Add-in Express™ Support Service
That's what is more important than anything else

How get content from Outlook.MailItem attachment
 
chen lu




Posts: 31
Joined: 2009-06-02
Outlook.MailItem.attachment only have method saveasfile, how can I get content not save to hard disk?
Posted 10 Jun, 2009 16:34:34 Top
Andrei Smolin


Add-in Express team


Posts: 19029
Joined: 2006-05-11
Hello Chen,

Please check the following sample project that uses Extended MAPI to process attachments:

http://www.add-in-express.com/files/projects_pub/oladdattachmentexample.zip


Andrei Smolin
Add-in Express Team Leader
Posted 11 Jun, 2009 06:59:26 Top
chen lu




Posts: 31
Joined: 2009-06-02
Thanks Andrei.

The example you give me does not tell me how to use MAPI to read the content of the attachment. Can you tell how to get the content using MAPI?
mapiObject = attch.MAPIOBJECT;
unkObj = Marshal.GetIUnknownForObject(mapiObject);
MAPI.HrGetOneProp(unk, MAPI.PR_ATTACH_SIZE, out pPropValue);????

Thank you very much!

Chen
Posted 11 Jun, 2009 09:21:46 Top
chen lu




Posts: 31
Joined: 2009-06-02
Thanks Andrei.

The sample you give me did not tell me how to get the content.
1.get MAPIObject
mapiObject = attch.MAPIOBJECT;
2. unkObj = Marshal.GetIUnknownForObject(mapiObject);
3. How to gent attachment content from unkObj?
MAPI.HrGetOneProp(unk, MAPI.PR_ATTACH_SIZE, out pPropValue);???

Thanks!
Posted 11 Jun, 2009 09:49:58 Top
Andrei Smolin


Add-in Express team


Posts: 19029
Joined: 2006-05-11
Hello Chen,

I've just used our free MAPI Store Accessor (http://www.add-in-express.com/products/mapi-store-events.php) to read an attachment as follows:


private AddinExpress.MAPI.ADXMAPIStoreAccessor adxmapiStoreAccessor1;
private AddinExpress.MSO.ADXOlInspectorCommandBar adxOlInspectorCommandBar1;
private AddinExpress.MSO.ADXCommandBarButton adxCommandBarButton1;
...
private void adxCommandBarButton1_Click(object sender)
{
    adxmapiStoreAccessor1.Initialize(false);
    Outlook.Inspector insp = OutlookApp.ActiveInspector();
    Outlook.MailItem outlookItem = insp.CurrentItem as Outlook.MailItem;
    AddinExpress.MAPI.MapiItem mapiItem = adxmapiStoreAccessor1.GetMapiItem(outlookItem);
    byte[] data = mapiItem.Attachments[0].GetProperty(AddinExpress.MAPI.ADXMAPIPropertyTag._PR_ATTACH_DATA_BIN) as byte[];
    System.Text.StringBuilder str = new System.Text.StringBuilder(data.Length);
    for (int i = 0; i < data.Length; i++)
    {
        str.Append(Convert.ToChar(data[i]));
    }
    //your stuff here
    Marshal.ReleaseComObject(outlookItem);
    Marshal.ReleaseComObject(insp);
    adxmapiStoreAccessor1.LogOff();
}



Andrei Smolin
Add-in Express Team Leader
Posted 12 Jun, 2009 08:14:02 Top
chen lu




Posts: 31
Joined: 2009-06-02
thank you!
Posted 12 Jun, 2009 23:58:07 Top