Outlook MailItem嵌入未在已发送邮件中显示的图像

问题描述 投票:0回答:1

我有以下代码将图像嵌入到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中查看我的已发送邮件文件夹时,图像仅显示如下:

Wrong shown image in 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);
   }
}

提前感谢。

c# image email outlook office-interop
1个回答
0
投票

我现在刚刚解决了这个问题。

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

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

      }
   }
© www.soinside.com 2019 - 2024. All rights reserved.