0

I have the following code to embed images to 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);
   }
}

That works just fine. The images are shown when i receive the e-mail - but just there.

When i take a look at my sent-mails folder in outlook the images are just shown like this:

Wrong shown image in outlook

Does anyone have an idea why they are shown like this and can help me to fix that?

I'm confused about that because the images are shown when i receive the e-mail.

Mail is send like this:

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

Thanks in advance.

Kenshinaro
  • 33
  • 8
  • In your code you replaced in the html with `ImageId={picture.Id.ToString()}` what is picture? picture doesnt seem mentioned in your code everywhere else its image – BugFinder Oct 31 '19 at 10:32
  • picture is image - issue in refactoring. Edited question – Kenshinaro Oct 31 '19 at 10:33
  • Does this answer your question? [VSTO Outlook Embed Image MailItem](https://stackoverflow.com/questions/4196160/vsto-outlook-embed-image-mailitem) – BugFinder Oct 31 '19 at 10:36
  • Your problem seems to be you arent adding the attachments with their Ids – BugFinder Oct 31 '19 at 10:37
  • No. I've shorten the code for this question. And i just forgot to change this string. Also the images are shown when i receive the e-mail. Just not in "sent-mails" folder. – Kenshinaro Oct 31 '19 at 10:39
  • It is really hard to help when the actual problem is not listed.. – BugFinder Oct 31 '19 at 10:40
  • The actual problem is in the question. "When i take a look at my sent-mails folder in outlook the images are just shown like this:". Means: The e-mail is sent with images and the images arrive and are shown in the folder of the receiver. But they are not shown in the folder of my sent e-mails. – Kenshinaro Oct 31 '19 at 10:42
  • But you arent giving us how to recreate this.. the code is incomplete, and constantly changing when pointing out issues with it.. – BugFinder Oct 31 '19 at 10:43
  • First of all the code is not constantly changing. I corrected one spelling. And I posted everything of the code that is needed to reproduce this. The rest has no connection to the problem. – Kenshinaro Oct 31 '19 at 10:48
  • Are the images shown correctly in the Sent Items folder after you restart Outlook? – Dmitry Streblechenko Oct 31 '19 at 14:55

1 Answers1

0

I just solved this issue right now.

For those who have the same problem here's my new method where i add the image references:

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

      }
   }
Kenshinaro
  • 33
  • 8