11

I'm currently developing an Outlook Addin which saves MailItems and Attachments in my MSSQL Database.

I got a method where I save the MailItem with all it's attachments. But if I save all attachments the embedded images in the MailItem are also saved.

Does anyone know how to save all real attachments?? I mean like the attachments in the picture below:

enter image description here

and not the embbeded images that are in the mail body.

Here is the code that I use to loop through all attachments of a MailItem and then save it:

foreach (Outlook.Attachment att in mailItem.Attachments)
{
      try
      {
          att.SaveAsFile(Path.GetTempPath() + att.FileName);

          var fi = new FileInfo(Path.GetTempPath() + att.FileName);

          //Saving attachment to DB
          var attachment = Attachment.NieuwAttachment(att.FileName, SelectedMap.DossierNr.ToString( CultureInfo.InvariantCulture), -1, Convert.ToInt32(SelectedMap.Tag), fi);
          if (!Attachment.InlezenAttachment(attachment)) continue;

          OutlookCategories.AddAttachmentCategory(mailItem);
      }
      catch (Exception ex)
      {
          var dmsEx = new DmsException("Er is een fout opgetreden bij het opslaan van een bijlage.", ex.Message, ex);
          ExceptionLogger.LogError(dmsEx);
      }
 }

Thanks!

----------- EDIT ------------

I also posted this question on the Microsoft TechNet and I just received an answer to the question (See link below)

Outlook 2007 & 2010: Save all attachments except the embedded attachments C#

----------- EDIT ------------

My problem is still not fixed, the help I got from Microsoft is useless.. So Please I really need this to be fixed!

ValarmorghulisHQ
  • 989
  • 1
  • 11
  • 31

3 Answers3

3

Use this code answered here :

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);
                    }

                }

            }
        }
Community
  • 1
  • 1
Aamol
  • 1,149
  • 1
  • 15
  • 23
0

Depends on how you define 'real' or 'proper' attachments. I'm going to assume you want to disregard all images that are embedded in the email. These are also attachments but are referenced in the actual body of the html email.

See this answer for an explanation on how attachments are embedded. The key is to disregard attachments that have a Content-ID value that is referenced by an image tag within the body of the email itself.

Community
  • 1
  • 1
Neil Trodden
  • 4,724
  • 6
  • 35
  • 55
  • 2
    Okay, I got the part where you said: Disregard attachments that have a Content-ID, but in C# and the OOM there is no Content-ID on an attachment object... So it makes it kinda hard... – ValarmorghulisHQ Dec 06 '13 at 13:17
  • 1
    That's a great point! Is there no way to get access to the raw email itself? – Neil Trodden Dec 06 '13 at 13:30
  • 1
    The only way to access the raw email is by converting it to mhtml, and the dig into the mhtml file but then the attachments (the attachments that I need) are disposed.... So in short, I don't think there is a way to access the raw mail without loosing my attachments, or by using the .msg file itself... – ValarmorghulisHQ Dec 06 '13 at 13:35
  • 1
    You cannot simply disregard all attachments that have the Content-ID MIME header set - e.g. Lotus Notes sets that header for all attachments. You really need to load the HTML body and check if any messages reference the attachments either by contents id, file name, or location. – Dmitry Streblechenko Dec 10 '13 at 13:43
  • i am also facing the same problem.please suggest me a solution – Ashu Dec 17 '14 at 04:44
0

This worked for me:

var test = attachments[i].PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
if (string.IsNullOrEmpty((string)test))
{
       //attachment
}
else
{
       //embedded image
}
RMK
  • 877
  • 1
  • 9
  • 16