MimeKit: how to embed images?

I am using MailKit / MimeKit 1.2.7 (latest version of NuGet).

I tried to embed the image in the HTML body of my letter, following the sample API documentation (section "Using BodyBuilder").

My current code is as follows:

var builder = new BodyBuilder(); builder.HtmlBody = @"<p>Hey!</p><img src=""Image.png"">"; var pathImage = Path.Combine(Misc.GetPathOfExecutingAssembly(), "Image.png"); builder.LinkedResources.Add(pathLogoFile); message.Body = builder.ToMessageBody(); 

I can send this letter, and actually an image attached to the letter. But it is not .

Am I missing something? Or is it an Apple Mail error (is it the email client I use to receive email)?

I am grateful for any idea (and many thanks to Jeffrey Steadfest for providing such wonderful tools!).

Ingmar

+7
c # mimekit
source share
1 answer

Try some more:

 var builder = new BodyBuilder (); var pathImage = Path.Combine (Misc.GetPathOfExecutingAssembly (), "Image.png"); var image = builder.LinkedResources.Add (pathLogoFile); image.ContentId = MimeUtils.GenerateMessageId (); builder.HtmlBody = string.Format (@"<p>Hey!</p><img src=""cid:{0}"">", image.ContentId); message.Body = builder.ToMessageBody (); 

If this works for you, I will update the documentation.

The problem may be that the Apple implementation of multipart/related does not resolve the Image.png link using the Content-Location header on the mime part of the image (possibly because it is a relative URL).

The cid: URL type should work, but it's a little harder to build, since you need to know the Content-Id values โ€‹โ€‹for each image attachment.

+14
source share

All Articles