c# - 如何删除作为附件的图像但显示在电子邮件正文中

标签 c# asp.net email email-attachments

我找到了在电子邮件正文中显示图像的解决方案: 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/

相关文章:

c# - .Net 核心互操作性 Linux

javascript - 自定义错误消息未从 javascript ajax 返回自定义消息

asp.net - 如何验证 asp 中的文本框是否有不平衡的括号

c# - 我如何对这个简单的表单进行单元测试

c# - 检索 Linkedin 电子邮件地址

c# - 停止引发 Filewatcher 控件的创建事件以自动生成原始内容的临时文件

c# - 代码运行时的 ASP.NET 应用程序日志/进度条

c# - IIS 反向代理干扰重定向位置 header

email - Facebook 在电子邮件中分享

actionscript-3 - 如何使按钮自动使用 AS3 发送电子邮件