c# - .NET 如何将图像附加到电子邮件的 html 模板中
问题描述
我正在尝试将图像附加到用于发送电子邮件的 html 模板中。我试图使其工作的代码如下所示:
string body = System.IO.File.ReadAllText("./Resources/templates/myTemplate.html");
byte[] imageArray = System.IO.File.ReadAllBytes(@"logo.jpg");
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
body = body.Replace("{imageBase64}", base64ImageRepresentation);
myTemplate.html 代码是:
<html>
<head></head>
<body>
<p>This is the email text</p>
<img src={imageBase64}/>
</body>
</html>
但是电子邮件没有发送。如果我删除与图像相关的代码,则电子邮件将正确发送。
有谁知道如何解决它?
解决方案
src 的格式需要是
src="data:image/png;base64, {imageBase64}"
如果这不起作用,请考虑将字符集添加到图像 src
src="data:image/png;charset=utf-8;base64, {imageBase64}"
推荐阅读
- ruby-on-rails - 获取嵌套资源 id 表单 URL
- outlook-addin - Outlook 自定义 Web 加载项移动应用程序问题:不适用于少数帐户
- javascript - Angular 7 使用 cardinity-js
- ios - 为什么我的 UITabBar 在载入屏幕加载后消失?
- java - Apache poi XSSFSheet.write() 未完成
- javascript - 固定椭圆上的动画角度
- java - 如何打印特定列的值
- forms - 无法在表单中同时发送文件和对象
- azure - 如何在 azure 资源模板中编写带有新警报的 webtest?
- scala - Scala Spark DataFrame Map Encoder 非原始类型