【问题标题】:Extract Inline Images with EWS使用 EWS 提取内联图像
【发布时间】:2018-02-15 12:42:51
【问题描述】:

我正在使用 EWS 从 Exchange 2013 提供的电子邮件中读取和提取图像。使用下面的代码,当图像作为实际附件附加时效果很好。当图像作为内联附件出现时,就会出现问题。

EWS .Hasattachments 不会为内联附件返回 true。这似乎很愚蠢。阅读下面的文章似乎可以解决问题,但我只是想知道什么是最好的、最有效的检索常规和内联图像附件并将它们保存到目录的方法。

https://social.technet.microsoft.com/Forums/office/en-US/ad10283a-ea04-4b15-b20a-40cbd9c95b57/exchange-2010-ews-c-display-html-email-problem-with-image?forum=exchangesvrdevelopment

  if (mail.HasAttachments && mail.Attachments[0] is FileAttachment)
                        {
                            int count = 0;
                            foreach (Attachment attachment in mail.Attachments)
                            {
                                if (attachment is FileAttachment && attachment.ContentType == "image/jpeg")
                                {
                                    Console.WriteLine(attachment.Name);
                                    FileAttachment fileAttachment = attachment as FileAttachment;
                                    string imagename = s + "-" + System.DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";

                                    /* download attachment to folder */
                                    fileAttachment.Load(imageLocation + "\\Images\\" + imagename);

}

【问题讨论】:

  • 我将代码更改为以下,但每次在 Error itemNot Found 的 .load 语句上都会抛出一个错误。
  • Console.WriteLine(attachment.Name);字符串 sID = 附件.ContentId; sType = sType.Replace("image/", "");字符串 sFilename = 附件。名称;字符串 sPathPlusFilename = Directory.GetCurrentDirectory() + "\\" + sFilename; ((FileAttachment)attachment).Load(sFilename);

标签: c# exchangewebservices


【解决方案1】:

您的第一行似乎从this MSDN article 获得了一些灵感。您不需要检查HasAttachments,只需自己遍历mail.Attachments

foreach (var attachment in mail.Attachments.Where(a => a is FileAttachment && a.ContentType == "image/jpeg"))
{
    Console.WriteLine(attachment.Name);
    FileAttachment fileAttachment = attachment as FileAttachment;
    string imagename = s + "-" + System.DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";

    /* download attachment to folder */
    fileAttachment.Load(imageLocation + "\\Images\\" + imagename);
}

我还没有测试过以上,但这应该只适用于任何 jpeg FileAttachment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-20
    • 2012-02-17
    • 2012-09-03
    • 2020-03-14
    • 2020-08-19
    • 1970-01-01
    • 2019-08-26
    • 2013-02-19
    相关资源
    最近更新 更多