use*_*789 11 c# asp.net email email-attachments
我找到了这个解决方案,用于在电子邮件正文中显示图像: 将图像添加到电子邮件正文中
它工作正常,但它也添加图像作为电子邮件的附件.
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>";
Run Code Online (Sandbox Code Playgroud)
有一行代码,评论显示为内联而非附件,但此行无法正常工作,因为图片仍会附加到电子邮件中:
//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
Run Code Online (Sandbox Code Playgroud)
如何阻止图像附加到电子邮件?
使用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);
Run Code Online (Sandbox Code Playgroud)
PS将图像作为base24字符串嵌入不是一个好主意,因为许多邮件客户端不支持这种功能。
另一种可以使用的替代方案是内联嵌入图像,但通常不会被电子邮件客户端过滤(当今垃圾邮件等情况通常如此),您可以使用 DataURL。
<img src="data:image/<type>;base64,<string>"/>
Run Code Online (Sandbox Code Playgroud)
其中<type>是图像类型,即 jpg、gif、png, 是 base64 字符串。只需将图像转换为 Base64 字符串并使用上述语法将其分配给源即可
例如,使用 jpeg...
mailMsg.Body = "<html><body> <img height=\"49\" width=\"169\" src=\"data:image/jpg;base64,<myPictureString>"\"> </body></html>";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2656 次 |
| 最近记录: |