1

I am trying to find a solution to distinguish between an embedded image and attachment in Outlook mail. After doing some research, i found the following code works for most of the case

foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
    try
    {
        var attachmentType = attachment.FileName.Substring(attachment.FileName.LastIndexOf('.'));
        if (attachmentType!=null&&attachmentType.Trim().Length>1&&_fileTypeFilter.Contains(attachmentType.Substring(1).ToLower()))
        {
            prop=attachment.PropertyAccessor;
            string conentId = (string)prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
            if ((attachmentType.Substring(1).ToLower() == "pdf") ||(conentId==null||conentId.Trim().Length==0))

            {          
                //Always allow PDF 
                // This is an attachement
            }
        }
    }
    catch (Exception ex)
    {

    }
}

The issue is when a mail is send from other mail systems (for eg: hotmail ) then the content id is not null for attachments. This cause the attachments to be ignored .

Another suggestion i tired is to check the property based on following StackFlow don't save embedded

foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
    try
    {
        // var tst = attachment.Type;
        var attachmentType = attachment.FileName.Substring(attachment.FileName.LastIndexOf('.'));
        if (attachmentType!=null&&attachmentType.Trim().Length>1&&_fileTypeFilter.Contains(attachmentType.Substring(1).ToLower()))
        {
            prop=attachment.PropertyAccessor;
            string conentId = (string)prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
            var flags = prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003"); 

            var asize = attachment.Size;
            if ((attachmentType.Substring(1).ToLower() == "pdf") ||
               (asize>0&&(flags!=4 &&(int)attachment.Type != 6)))  // As per present understanding - If rtF mail attachment comes here - and the embeded image is treated as attachment then Type value is 6 and ignore it
                                                                                             //  (conentId==null||conentId.Trim().Length==0))

            {
              //This is a valid attachment
            }
        }
    }
    catch (Exception ex)
    {

    }
}

But this sometimes includes the image in signature

Aderbal Farias
  • 989
  • 10
  • 24
makdu
  • 908
  • 2
  • 13
  • 26
  • work with file types - see vba example https://stackoverflow.com/a/43180639/4539709 – 0m3r Apr 02 '19 at 00:16

3 Answers3

0

the most reliable way is to parse the HTML body (MailItem.HTMLBody property) to extract all img tags and check their scr attributes. If its is of the form ""cid:xyz", then "xyz" will be the value of the PR_ATTACH_CONTENT_ID property on the attachment. It can also refer to the graphics file by its file name.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

Here is the working solution

 var selection = explorer.Selection;

            if (selection.Count == 1)
            {
                object selectedItem = selection[1];

                var mailItem = selectedItem as Outlook.MailItem;
                if (mailItem == null) return;


              foreach (Outlook.Attachment attachment in mailItem.Attachments)
                 {

                  bool validAttachment = isAnAttachment(attachment);
                  }       
            }


 private bool isAnAttachment(Outlook.Attachment attachment)
                {
                    bool isValid = false;
                    var attachmentType = attachment.FileName.Substring(attachment.FileName.LastIndexOf('.'));
                    if (attachmentType != null && attachmentType.Trim().Length > 1 && _fileTypeFilter.Contains(attachmentType.Substring(1).ToLower()))
                    {
                        Outlook.PropertyAccessor prop = attachment.PropertyAccessor;

                        var flags = prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003");

                        var asize = attachment.Size;
                        // As per present understanding - If rtF mail attachment comes here - and the embeded image is treated as attachmet
                        if ((attachmentType.Substring(1).ToLower() == "pdf") || (asize > 0 && flags != 4 && (int)attachment.Type != 6))
                        {
                            isValid = true;
                        }
                    }
                    return isValid;
                }
makdu
  • 908
  • 2
  • 13
  • 26
0

I guess this will work:

((int)attachment.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003") != 4 &&  (int)attachment.Type != 6)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit](https://stackoverflow.com/posts/76268762/edit) to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken May 21 '23 at 08:25