【问题标题】:Saving only the REAL attachments of an Outlook MailItem仅保存 Outlook MailItem 的真实附件
【发布时间】:2013-12-23 19:29:36
【问题描述】:

我目前正在开发一个 Outlook 插件,它将邮件项和附件保存在我的 MSSQL 数据库中。

我有一个方法可以保存 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: Save all attachments except the embedded attachments C#

----------- 编辑 ------------

我的问题还没有解决,我从微软那里得到的帮助是没有用的。所以我真的需要解决这个问题!

【问题讨论】:

  • 你现在的代码在哪里?

标签: c# outlook attachment


【解决方案1】:

使用此代码回答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);
                    }

                }

            }
        }

【讨论】:

    【解决方案2】:

    这对我有用:

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

    【讨论】:

    • 我试过这个。这对我不起作用。它甚至可以将真正的附件识别为嵌入的。
    【解决方案3】:

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

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

    【讨论】:

    • 好的,我得到了你所说的部分:忽略具有 Content-ID 的附件,但在 C# 和 OOM 中,附件对象上没有 Content-ID...所以它做到了有点难……
    • 这很好!有没有办法访问原始电子邮件本身?
    • 访问原始电子邮件的唯一方法是将其转换为 mhtml,然后挖掘到 mhtml 文件,然后处理附件(我需要的附件)......所以简而言之,我认为没有办法在不丢失附件或使用 .msg 文件本身的情况下访问原始邮件...
    • 您不能简单地忽略所有设置了 Content-ID MIME 标头的附件 - 例如Lotus Notes 为所有附件设置该标题。您确实需要加载 HTML 正文并检查是否有任何消息通过内容 ID、文件名或位置引用附件。
    • 我也面临同样的问题。请给我一个解决方案
    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 2010-12-24
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    相关资源
    最近更新 更多