如何使用Windows应用程序在html电子邮件模板中添加图像

时间:2018-03-12 11:22:03

标签: c# asp.net windows winforms

我正在创建一个Windows应用程序,我将在其中发送emailtemplate.htm文件。

我想知道如何在html模板中加载图片。

  

我试过了

  1. 尝试使用img src添加图片无效。
  2. TestEmail.html:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
        <title>Welcome to GrassDew</title>
    </head>
    <body>
        <div style="border-top:3px solid #22BCE5">&nbsp;</div>
        <span style="font-family:Arial;font-size:10pt">
            A new article has been published .<br /><br />
            <a style="color:#22BCE5" href="https://www.google.com/">NEw Article</a><br />
            <br /><br />
        </span>
        Thanks,<br />
        Team<br />
        <img src="../images/signature.png" /><br /><br />
    </body>
    </html>
    
      

    CS代码:

            string strBody = string.Empty;
            Assembly assembly = Assembly.GetExecutingAssembly();
            string assemblyName = assembly.GetName().Name;
            string emailTemplateName = "TestEmail.htm";
            emailTemplateName = assemblyName + "." + emailTemplateName;
            //StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("oMail.Win.TestEmail.htm"));
            using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(emailTemplateName)))
            {
                strBody = reader.ReadToEnd();
            }
            strMsg.Body += "<span style = font-family:Calibri;font-size:14pt>Dear " + customrname + "</span><br/>" + strBody;
            strMsg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
            System.Net.NetworkCredential smtpCredential = new System.Net.NetworkCredential(strSMTPServerHostUserName, strSMTPServerHostPassword, strSMTPServerDomain);
            SmtpClient smtpClient = new SmtpClient(strSMTPServerHost, Convert.ToInt32(strSMTPServerPort));
            smtpClient.Credentials = smtpCredential;
            smtpClient.Send(strMsg);
            bEmailSentSuccessful = true;
    

1 个答案:

答案 0 :(得分:5)

您可以使用base64字符串作为图片来源。它就像是html中的嵌入式图片。

您的src应该是

src="data:image/jpeg;base64, LzlqLzRBQ...<!-- base64 data -->"

例如:

 Image Red Dot <img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
    9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />

您可以在google中找到如何将图片转换为base64字符串。例如,请检查此link

修改

当你发送html作为电子邮件../images/signature.png将无法解决时,它应该是服务器的路径,如“http://myserver/images/image.jpeg”或base64字符串。