【问题标题】:How to avoid embedded (inline) images when saving outlook email attachments保存 Outlook 电子邮件附件时如何避免嵌入(内嵌)图像
【发布时间】:2015-10-09 03:02:47
【问题描述】:

我写了一个方法来将outlook邮件附件保存到硬盘,并将文件转换成Base64String。当我保存附件时,嵌入的(内联)图像也会被保存,即使它们不是真正的附件。我只想保存真正的附件。然后我将方法修改如下。但是现在我从“.OfType()”行中得到一个错误。

这是我的代码:

private string GetBase64StringForAttachments(Outlook.MailItem mailItem)
        {
            StringBuilder builder = new StringBuilder();
            Outlook.Attachments mailAttachments = mailItem.Attachments;
            
            try
            {                
                if (mailAttachments != null)
                {                    
                    Regex reg = new Regex(@"<img .+?>", RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                    MatchCollection matches = reg.Matches(mailItem.HTMLBody);

                    for (int i = 1; i <= mailAttachments.Count; i++)
                    {
                        Outlook.Attachment currentAttachment = mailAttachments[i];

                        bool isMatch = matches
                          .OfType<Match>()
                          .Select(m => m.Value)
                          .Where(s => s.IndexOf("cid:" + currentAttachment.FileName, StringComparison.InvariantCultureIgnoreCase) >= 0)
                          .Any();

                        MessageBox.Show(currentAttachment.FileName + ": " + (isMatch ? "Inline Image" : "Attached Image"));

                        if (currentAttachment != null)
                        {
                            
                            string date = DateTime.Now.ToString("yyyymmddhhmmss");
                            string path = "C:\\test\\" + date + currentAttachment.FileName; //ToDo: Create Folder
                            currentAttachment.SaveAsFile(path);

                            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                            byte[] filebytes = new byte[fs.Length];
                            fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
                            string encodedData = Convert.ToBase64String(filebytes, Base64FormattingOptions.InsertLineBreaks);
                            builder.Append(encodedData).Append(",");

                            Marshal.ReleaseComObject(currentAttachment);
                        }
                    }

                    if (builder.Length > 0)
                    {                        
                        string encodedAttachments = builder.ToString().Remove(builder.ToString().Length-1);
                        return builder.ToString();
                    }
                    else
                        return "";

                }
                else return "";
            }
            catch (Exception ex)
            {
                Debug.DebugMessage(2, "Error in GetBase64StringForAttachments : in AddinModule " + ex.Message);
                return "";
            }
            finally
            {
                Marshal.ReleaseComObject(mailAttachments);                
            }
        }

这是错误消息:

“System.Text.RegularExpressions.MatchCollection”不包含“OfType”的定义,并且找不到接受“System.Text.RegularExpressions.MatchCollection”类型的第一个参数的扩展方法“OfType”(您是否缺少using 指令还是程序集引用?)

我需要什么:

  1. 我不是 LINQ 的忠实粉丝。所以,请你给我建议

  2. 有更好的方法吗?

    我已经尝试按照这些问题的建议答案进行操作,但它们对我不起作用

Saving only the REAL attachments of an Outlook MailItem

Don't save embed image that contain into attachements (like signature image)

  1. 有没有办法使用救赎来区分真正的附件?

【问题讨论】:

  • 如果您无法使用标志解决方案,请注意 Microsoft 更改了标志的规则,您必须为您的检查使用不同的值才能使其在 Outlook 365 中工作。有关详细信息,请参阅这个答案stackoverflow.com/a/70823130/1657610.
  • @RegEdit,非常感谢您的关注以及您与我分享的答案。

标签: c# linq outlook outlook-addin outlook-redemption


【解决方案1】:

是的,Redemption 公开了 RDOAttachment.Hidden 属性 - 它会检查 HTMLBody 以确保附件未被用作内嵌图像。

另请注意,您可以使用RDOAtttachment.AsArray 访问附件数据,而无需先将附件保存为文件。

Redemption.RDOSession rSession = new Redemption.RDOSession();
rSession.MAPIOBJECT = mailItem.Application.Session.MAPIOBJECT;
Redemption.RDOMail rMail= rSession.GetRDOFolderFromOutlookObject(mailItem)
foreach (Redemption.RDOAttachment attach in rMail.Attachments)
{
  if ((attach.Type == Redemption.rdoAttachmentType.olByValue) && (!attach.Hidden))
  {
     attach.SaveAsFile(path);
  }
}
next

【讨论】:

    【解决方案2】:
    using Attachment = MsgReader.Outlook.Storage.Attachment;
    
    foreach (Attachment attachment in mailItem.Attachments.Where(a => ((Attachment)a).Hidden == false)) {
      // do whatever you want with the 'real' attachments.
    }
    

    【讨论】:

      猜你喜欢
      • 2022-09-25
      • 2012-03-30
      • 1970-01-01
      • 2020-09-12
      • 2010-10-01
      • 2016-02-22
      • 2010-10-29
      • 2011-10-30
      • 2021-12-23
      相关资源
      最近更新 更多