Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving only the REAL attachments of an Outlook MailItem

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!

like image 915
ValarmorghulisHQ Avatar asked Dec 06 '13 12:12

ValarmorghulisHQ


People also ask

How do I save only attachments in Outlook?

Save one or more attachments Click the attachment in the Reading Pane or the open message. On the Attachments tab, in the Actions group, click Save As. You can also right-click the attachment, and then click Save As. To select multiple attachments, hold down the Ctrl key while clicking the attachments.

How do I change the attachment settings in Outlook?

Once Outlook is loaded and you are in your Inbox, click the gear icon in the top-right corner, which is your settings. Click View All Outlook Settings → Attachments. Under Sharing Preferences, you can select from several different options.


1 Answers

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

                }

            }
        }
like image 62
Aamol Avatar answered Oct 06 '22 18:10

Aamol