首页 > 解决方案 > .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>

但是电子邮件没有发送。如果我删除与图像相关的代码,则电子邮件将正确发送。

有谁知道如何解决它?

标签: c#.net

解决方案


src 的格式需要是

src="data:image/png;base64, {imageBase64}"

如果这不起作用,请考虑将字符集添加到图像 src

src="data:image/png;charset=utf-8;base64, {imageBase64}"

推荐阅读