仅保存Outlook MailItem的REAL附件

我目前正在开发一个Outlook Addin,它可以在我的MSSQL数据库中保存MailItems和Attachments。

我有一个方法,我用它的所有附件保存MailItem。 但是,如果我保存所有附件,也会保存MailItem中的嵌入图像。

有谁知道如何保存所有真正的附件? 我的意思是像下图中的附件:

在此处输入图像描述

而不是邮件正文中的嵌入图像。

这是我用来循环遍历MailItem的所有附件然后保存它的代码:

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

谢谢!

———–编辑————

我还在Microsoft TechNet上发布了这个问题,我刚刚收到了问题的答案(参见下面的链接)

Outlook 2007和2010:保存除嵌入式附件C#以外的所有附件

———–编辑————

我的问题仍然没有解决,我从微软获得的帮助是没用的。所以,我真的需要这个修复!

使用此代码回答:

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

取决于您如何定义“真实”或“正确”的附件。 我将假设您要忽略电子邮件中嵌入的所有图像。 这些也是附件,但在html电子邮件的实际正文中引用。

有关如何嵌入附件的说明,请参阅此答案。 关键是忽略具有Content-ID值的附件,该附件由电子邮件本身的图像标签引用。

这对我有用:

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