我找到了在电子邮件正文中显示图像的解决方案: Add image to body of an email
它工作正常,但它还会将图像作为附件添加到电子邮件中。
Attachment inlineLogo = new Attachment(EmailLogo.ImageUrl);
mailMsg.Attachments.Add(inlineLogo);
string contentID = "Image";
inlineLogo.ContentId = contentID;
//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
//To embed image in email
mailMsg.Body = "<htm><body> <img height=\"49\" width=\"169\" src=\"cid:" + contentID + "\"> </body></html>";
有一行代码将评论显示为内联而不是附件,但这行代码不起作用,因为图像仍然附加到电子邮件中:
//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
如何阻止图像附加到电子邮件?
最佳答案
使用AlternateView将嵌入图像的 html 代码存储为 LinkedResource :
string contentID = "Image";
var inlineLogo = new LinkedResource(EmailLogo.ImageUrl, "image/png");
inlineLogo.ContentId = contentID;
var htmlView = AlternateView.CreateAlternateViewFromString(
"<html><body> <img height=\"30\" width=\"30\" src=\"cid:" + contentID + "\"> </body></html>",
Encoding.UTF8, "text/html");
htmlView.TransferEncoding = TransferEncoding.QuotedPrintable;
htmlView.LinkedResources.Add(inlineLogo);
mailMsg.AlternateViews.Add(htmlView);
附言将图像嵌入为 base24 字符串不是一个好主意,因为许多邮件客户端不支持这种能力。
关于c# - 如何删除作为附件的图像但显示在电子邮件正文中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41446280/