Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

c# - MimeKit: How to embed images?

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

I tried to embed an image in the HTML body of my email by following the sample from the API documentation (section "Using a BodyBuilder").

My current code looks like this:

 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 email and in fact the image is attached to the email. But it is not embedded.

Am I missing something? Or is this Apple Mail's fault (this is the email client I am using for receiving emails)?

I am grateful for any idea (and thanks so much to Jeffrey Stedfast for providing such a great toolset!!).

Ingmar

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try something a bit more like this:

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'll update the documentation.

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

The cid: URL-type should work, though, but it's a bit more awkward to construct since you need to know the Content-Id values for each image attachment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...