Sicos1977 / MsgKit

A .NET library to make MSG files without the need for Outlook
206 stars 56 forks source link

Embedded images don't seem to appear when viewed in Outlook #128

Closed krugertech closed 5 months ago

krugertech commented 5 months ago

Hi There. Thanks for this great library.

When I create a new email and embed/inline an image, the image does not seem to appear when viewing the .msg file in Outlook.

image

OutlookSpy can detect the image attachment though.

image

Here is the code I used. I tested different .jpg files and tried setting different rendering positions in an effort to get the embedded image to display in Outlook without success.

    static void TestInlinedAttachment()
    {
        using (var email = new Email(
                new Sender("peterpan@neverland.com", "Peter Pan"),
                new Representing("tinkerbell@neverland.com", "Tinkerbell"),
                "Hello Neverland subject"))
        {
            email.Recipients.AddTo("captainhook@neverland.com", "Captain Hook");
            email.Recipients.AddCc("crocodile@neverland.com", "The evil ticking crocodile");
            email.Subject = "This is the subject";
            email.BodyText = "Hello Neverland text";
            email.BodyHtml = "<html><head></head><body><b>Hello Neverland html</b></body></html>";
            email.Importance = MsgKit.Enums.MessageImportance.IMPORTANCE_HIGH;
            email.IconIndex = MessageIconIndex.ReadMail;
            email.Attachments.Add(File.OpenRead(@"D:\IMG-20240524-WA0000.jpg"),"img.jpg", -1, isInline: true, "img.jpg");
            email.Save(@"d:\email.msg");
        }
    }
Sicos1977 commented 5 months ago

This is not how you embed inline images. You need to add an image tag into your html and set the image source to cid:[the id you have given your inline image]

It will be something like this

<img src="cid:some-image-cid" />

When your msg file is opened by Outlook it will see that the source begins with cid (Content ID) and then reads the id that is behind the : ... it will then search if there are any inline attachments with this id and use that image to display.

Sicos1977 commented 5 months ago
<html>
   <head>
      <title>Peter Pann</title>
   </head>
   <body>
      <b>Hello Neverland html</b><br/>
     <img src="cid:img.jpg">
   </body>
</html>