Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vsto + differentiate attachments

I need to get and save the attachments(s) from a mail item, but using the code below returns all attachments - meaning it also returns the embedded images like the sender's signature with logo which is an image. How can I differentiate a true attachment vs. embedded images? I have seen a lot from forums but it is still unclear to me.

public static void SaveData(MailItem currentMailItem)
{
    if (currentMailItem != null)
    {       
        if (currentMailItem.Attachments.Count > 0)
        {
            for (int i = 1; i <= currentMailItem.Attachments.Count; i++)
            {
                currentMailItem.Attachments[i].SaveAsFile(@"C:\TestFileSave\" + currentMailItem.Attachments[i].FileName);
            }
        }
    }   
}
like image 433
Liz Avatar asked Sep 04 '12 02:09

Liz


1 Answers

You can check whether an attachment is inline or not by using the following pseudo-code from MS Technet Forums.

if body format is plain text then
   no attachment is inline
else if body format is RTF then
   if PR_ATTACH_METHOD value is 6 (ATTACH_OLE) then
     attachment is inline
   else
     attachment is normal
else if body format is HTML then
   if PR_ATTACH_FLAGS value has the 4 bit set (ATT_MHTML_REF) then
     attachment is inline
   else
     attachment is normal

You can access the message body format using MailItem.BodyFormat and the MIME attachment properties using Attachment.PropertyAccessor.

string PR_ATTACH_METHOD = 'http://schemas.microsoft.com/mapi/proptag/0x37050003';
var attachMethod = attachment.PropertyAccessor.Get(PR_ATTACH_METHOD);

string PR_ATTACH_FLAGS = 'http://schemas.microsoft.com/mapi/proptag/0x37140003';
var attachFlags = attachment.PropertyAccessor.Get(PR_ATTACH_FLAGS);
like image 99
SliverNinja - MSFT Avatar answered Oct 15 '22 16:10

SliverNinja - MSFT