将我的电脑上的图像保存为HTML以嵌入到电子邮件中? [英] Save image on my computer as HTML to embed in email?
问题描述
我试图将我电脑上的图片转换为HTML格式,并将其嵌入到电子邮件中:
这是我迄今为止所尝试的:
chart1.SaveImage(C:\\ My Chart \\mychart.png,ChartImageFormat.Png) ;
OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.Subject =这是主题;
var bytes = File.ReadAllBytes(C:\\ My Chart \\mychart.png);
var b64String = Convert.ToBase64String(bytes);
var dataUrl =data:image / png; base64,+ b64String;
mailItem.HTMLBody = dataUrl;
//将消息设置为高优先级
mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.To =email@email.com;
mailItem.Display(false);
这不起作用,它显示为实际电子邮件中的一堆代码
构建适当的HTML 以HTML格式发送主体,请尝试构建一个< img>
标签并将它的 src
设置为您的Base 64编码的图像数据:
//将您的电子邮件正文设置为包含Base64编码图像的图像
mailItem.HTMLBody = String.Format(< img src ='{0}'/>,dataUrl);
这会建立您的< img>
标记如预期,但是如果出现或不出现将是一个支持问题。
检查浏览器和客户端支持
值得注意的是,
I'm trying to convert an image I have on my computer to HTML and embed it into an email:
This is what I've tried so far:
chart1.SaveImage("C:\\My Chart\\mychart.png", ChartImageFormat.Png);
OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.Subject = "This is the subject";
var bytes = File.ReadAllBytes("C:\\My Chart\\mychart.png");
var b64String = Convert.ToBase64String(bytes);
var dataUrl = "data:image/png;base64," + b64String;
mailItem.HTMLBody = dataUrl ;
//Set a high priority to the message
mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.To = "email@email.com";
mailItem.Display(false);
This doesn't work, it shows up as a bunch of code in the actual email
Building Proper HTML
Since you appear to be sending the body as HTML, try building an <img>
tag and setting the src
of it to your Base 64 encoded image data :
// Set the body of your email to an image that contains your Base64-encoded image
mailItem.HTMLBody = String.Format("<img src='{0}' />",dataUrl);
This should build your <img>
tag as expected, however if will appear or not will be a matter of support.
Checking Browser and Client Support
It's worth noting that while most major browsers will support Base64 URLs, many have issues and limitations. Additionally, some e-mail clients may not support them either as seen below :
这篇关于将我的电脑上的图像保存为HTML以嵌入到电子邮件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!