这个问题已经提出并在SO上的几个地方回答了,特别是在这里:Gmail的问题-使用MailKit嵌入图像并处理我的确切查询,但答案似乎不适用于我的情况。
我尝试构建一个简单的邮件程序,将图像嵌入HTML电子邮件中,但是在多个客户端中,所有图像仍最终以附件形式出现。
我怀疑是我处理图片附件(一个.gif和几个.png)的方式有问题:
// Add pictures to embedded resources and replace links to pictures in the message
foreach (string imgpath in ImgPaths)
{
var image = builder.LinkedResources.Add(imgpath);
image.ContentId = MimeUtils.GenerateMessageId();
image.ContentDisposition = new ContentDisposition() { Disposition = ContentDisposition.Inline };
HtmlFormat = HtmlFormat.Replace(Path.GetFileName(imgpath), string.Format("cid:{0}", image.ContentId));
}
builder.HtmlBody = HtmlFormat;
message.Body = builder.ToMessageBody();
我可以从电子邮件(例如,在Gmail中收到)中看到,确实显示图像被列为:
src="images/cid:TPSXHQUDSAU4.JJRKUIEGLTZ5@Loralei"
我尝试过的两个客户端(gmail和roundcube)都将所有图像作为附件附加。我已经遍历了您的教程,并在这里查看了所有内容:https : //stackoverflow.com/search? q = Mailkit+Embed不幸的是,我似乎找不到我的错误。希望@jstedfast可以看到我在哪里犯了错误?
更新
如@jstedfast所述,更正后的代码应该是(就我而言):
foreach (string imgpath in ImgPaths)
{
var test = Path.GetFileName(imgpath);
var image = builder.LinkedResources.Add(imgpath);
image.ContentId = MimeUtils.GenerateMessageId();
image.ContentDisposition = new ContentDisposition() { Disposition = ContentDisposition.Inline };
//HtmlFormat = HtmlFormat.Replace(Path.GetFileName(imgpath), string.Format("cid:{0}", image.ContentId));
HtmlFormat = HtmlFormat.Replace($"images/{test}", string.Format("cid:{0}", image.ContentId)); //use images/filename
}
这看起来像问题:
<img src="images/cid:TPSXHQUDSAU4.JJRKUIEGLTZ5@Loralei" .../>
必须是:
<img src="cid:TPSXHQUDSAU4.JJRKUIEGLTZ5@Loralei" .../>
我的猜测是,为了解决此问题,您需要更改以下代码行:
HtmlFormat = HtmlFormat.Replace(Path.GetFileName(imgpath), string.Format("cid:{0}", image.ContentId));
对此:
HtmlFormat = HtmlFormat.Replace(imgpath, string.Format("cid:{0}", image.ContentId));
希望有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句