通过电子邮件发送 HTML 文档,使用 c# 嵌入图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16442196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Email HTML document, embedding images using c#
提问by Marcel-Is-Hier
So here is my situation.
所以这是我的情况。
I need to send an email as a string in HTML format that has images embedded in it. I tried converting my images into base64 but it does not work.
我需要将电子邮件作为 HTML 格式的字符串发送,其中嵌入了图像。我尝试将我的图像转换为 base64,但它不起作用。
The email has 3 images as type System.Drawing.Image. I just need to get them in my html formatted string.
该电子邮件有 3 张图像,类型为 System.Drawing.Image。我只需要将它们放入我的 html 格式字符串中。
采纳答案by Dragos Bobolea
You were right about converting to base64, but it is not enough to embed it (there would be no way for the client to distinguish base64 to plain text), you need to format it a bit.
您转换为 base64 是正确的,但仅嵌入它是不够的(客户端无法将 base64 与纯文本区分开),您需要对其进行一些格式化。
Check out Buhake's answer, it covers very well the problem in general (you should be able to take it from there): How to embed images in email
查看 Buhake 的答案,它总体上很好地涵盖了问题(您应该可以从那里获取): 如何在电子邮件中嵌入图像
回答by Jagan
The other way to embed images in E-mail when using System.Net.Mailis to attach image from local drive to email and assign a contentIDto it and later use this contentIDin the image URL.
使用时在电子邮件中嵌入图像的另一种方法System.Net.Mail是将图像从本地驱动器附加到电子邮件并为其分配一个contentID,然后contentID在图像 URL 中使用它。
That can be done like this:
可以这样做:
msg.IsBodyHtml = true;
Attachment inlineLogo = new Attachment(@"C:\Desktop\Image.jpg");
msg.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
msg.Body = "<htm><body> <img src=\"cid:" + contentID + "\"> </body></html>";

