MimeKit:如何嵌入图像? [英] MimeKit: How to embed images?

查看:95
本文介绍了MimeKit:如何嵌入图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MailKit/MimeKit 1.2.7(最新的NuGet版本).

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").

我当前的代码如下:

 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.

我错过了什么吗?还是这是Apple Mail的故障(这是我用来接收电子邮件的电子邮件客户端)?

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

我对任何想法都很感激(非常感谢Jeffrey Stedfast提供了如此出色的工具集!!

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

英格玛

推荐答案

尝试一些类似的方法:

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.

问题可能是苹果的multipart/related实现无法使用图像哑剧部分上的Content-Location标头解析Image.png引用(可能是因为它是相对URL).

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

尽管cid: URL类型应该可以工作,但是构造起来有点麻烦,因为您需要了解每个图像附件的Content-Id值.

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.

这篇关于MimeKit:如何嵌入图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆