I work on a VSTO in c#. When I click on button I save attachment in a folder. My problem is : when I have a rich email with an image in the signature, I have a element in my attachment. But I don't want save that image. Outlook (application) hide this attachment in the area attachment ! So why not me :-(
My code is very simply :
MailItem MailItemSelected =  this.OutlookItem;   
foreach (Attachment a in MailItemSelected.Attachments)
{
   a.SaveAsFile(path + a.FileName);
}
But I don't find a test for don't save the image of the signature.
We had a need to show only the "Mail Attachments" (not the embedded ones that are used for rendering) in an Outlook Add - In, and this is what that works.
 if (mailItem.Attachments.Count > 0)
        {
            // get attachments
            foreach (Attachment attachment in mailItem.Attachments)
            {
                var flags = attachment.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003");
                //To ignore embedded attachments -
                if (flags != 4)
                {
                    // 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
                    if ((int)attachment.Type != 6)
                    {
                        MailAttachment mailAttachment = new MailAttachment { Name = attachment.FileName };
                        mail.Attachments.Add(mailAttachment);
                    }
                }
            }
        }
I've find one part of my solution. When you create an email the size of image embed is to 0. So you can exclude this.
But it is not right when I read a email.
MailItem MailItemSelected =  this.OutlookItem;   
foreach (Attachment a in MailItemSelected.Attachments)
{                                    
   if(a.Size != 0)
      a.SaveAsFile(path + a.FileName);
}
When I read email I found a solution, but it is not very nice. So I write it, but if anybody think have better, I like it. In my example I try to get the Flag property with the PropertyAccessor, if it's a embed image, it's ok else I've an exception that be raise.
MailItem MailItemSelected =  this.OutlookItem;   
foreach (Attachment a in MailItemSelected.Attachments)
{
   bool addAttachment = false;
   try
   {
      string schemaPR_ATTACH_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x37140003"; 
      a.PropertyAccessor.GetProperty(schemaPR_ATTACH_FLAGS);
   }
   catch
   {
      addAttachment = true;
   }
   if (addAttachment && (a.Size != 0))
      a.SaveAsFile(path + a.FileName);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With