【问题标题】:Outlook MailItem embed images not shown in sent mailsOutlook MailItem 嵌入图像未显示在已发送邮件中
【发布时间】:2019-11-05 00:19:28
【问题描述】:

我有以下代码将图像嵌入到Outlook MailItems

private void ReplaceImageIds()
{
   foreach(var image in Image.GetImagesFromText(HTMLBody))
   {
      var imageTag = $"<img src \" cid:{image.Id.ToString()} \"/>";

      var attachment = _mailItem.Attachments.Add(image.FilePath, OlAttachmentType.olEmbeddeditem, null, "");
      attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/png");
      attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", image.Id.ToString());

      HTMLBody = HTMLBody.Replace($"ImageId={image.Id.ToString()}", imageTag);
   }
}

效果很好。当我收到电子邮件时会显示图像 - 但就在那里。

当我在 Outlook 中查看我的已发送邮件文件夹时,图像显示如下:

有没有人知道他们为什么会这样显示并且可以帮助我解决这个问题?

我对此感到困惑,因为当我收到电子邮件时会显示图像。

邮件是这样发送的:

public Boolean Send()
{
   // Check if all properties are set.
   Validate();

   try
   {
      var oApp = new Application();

      var oNS = oApp.GetNamespace("mapi");

      oNS.Logon();

      _mailItem = oApp.CreateItem(OlItemType.olMailItem) as MailItem;

      // Set To, CC and BCC.
      AddRecipients();

      // Replace images.
      ReplaceImageIds();

      if (Body != null)
         _mailItem.Body = Body;

      if(HTMLBody != null)
         _mailItem.HTMLBody = HTMLBody;              

      _mailItem.Subject = Subject;

      // Set account to send.
      SetSendingAccount(oApp);

      // Add attachments.
      AddAttachments();

      _mailItem.Send();

      oNS.Logoff();

      return true;
   }
   catch (System.Exception ex)
   {
      Utils.LogException(ex, "Could not send email.");
      throw new System.Exception("Could not send email.", ex);
   }
}

提前致谢。

【问题讨论】:

  • 在你的代码中你用ImageId={picture.Id.ToString()}替换了html中的图片是什么?图片在你的代码中似乎没有在其他地方提到它的图片
  • picture is image - 重构中的问题。编辑问题
  • 这能回答你的问题吗? VSTO Outlook Embed Image MailItem
  • 您的问题似乎是您没有添加带有其 ID 的附件
  • 没有。我已经缩短了这个问题的代码。我只是忘了更改这个字符串。当我收到电子邮件时,也会显示图像。只是不在“已发送邮件”文件夹中。

标签: c# image email outlook office-interop


【解决方案1】:

我刚刚解决了这个问题。

对于那些有同样问题的人,这是我添加图像引用的新方法:

private void ReplaceImageIds()
{
   foreach (var image in Image.GetImagesFromText(HTMLBody))
   {
      var imageTag = $"<img src = \"cid:{image.Id.ToString()}\"/>";

      var attachment = _mailItem.Attachments.Add(image.FilePath, OlAttachmentType.olEmbeddeditem, 0, image.Name);

      attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpg");
      attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", image.Id.ToString());

      HTMLBody = HTMLBody.Replace($"ImageId={image.Id.ToString()}", imageTag);

      }
   }

【讨论】:

    猜你喜欢
    • 2011-05-10
    • 2016-12-22
    • 2020-06-08
    • 2016-10-30
    • 2018-01-31
    • 2013-10-15
    • 2021-06-26
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多