根据您的评论,您似乎正在尝试生成要通过电子邮件发送的 html 文件。你最好创建一个 html 文件place holders
来获取你想要注入的信息。使用文本阅读器从文件中读取 html,并在文本上使用 regex 或 string.replace 替换place holders
实际值。
您将需要一个完整的图像 URL。
例如
阅读 HTML
var HTML = EmailTemplate.GetHTMLFromTemplate(path to template)
public static string GetHTMLFromTemplate(string templatepath)
{
var TemplateBody = new StringBuilder();
using (var Reader = new StreamReader(templatepath))
{
string Line;
while ((Line = Reader.ReadLine()) != null)
{
TemplateBody.Append(Line);
}
}
return TemplateBody.ToString();
}
然后替换html的部分
String.Replace("[image placeholder]", ConfigurationManager.AppSettings["Image"]);
电子邮件模板类看起来像这样(这是非常古老的代码!!)
public class EmailTemplate
{
public string Body { get; private set; }
public static EmailTemplate GetTemplate(string body, string url, string html)
{
var AmendedHTML = new StringBuilder(html);
AmendedHTML.Replace("[body]", body);
AmendedHTML.Replace("../images", url + "images");
var MyEmailTemplate = new EmailTemplate { Body = AmendedHTML.ToString() };
return MyEmailTemplate;
}
public static EmailTemplate GetTemplate(string body, string title, string url, string year, string html)
{
var AmendedHTML = new StringBuilder(html);
AmendedHTML.Replace("[BODY]", body);
AmendedHTML.Replace("[HEADING]", title);
AmendedHTML.Replace("../images", url + "images");
AmendedHTML.Replace("[DATE]", year);
AmendedHTML.Replace("[contactus.aspx]", url + "contactus/index.aspx");
AmendedHTML.Replace("[unsubscribe.aspx]", url + "register/unsubscribe.aspx");
var MyEmailTemplate = new EmailTemplate { Body = AmendedHTML.ToString() };
return MyEmailTemplate;
}
public static string GetHTMLFromTemplate(string path)
{
var TemplateBody = new StringBuilder();
using (var Reader = new StreamReader(path))
{
string Line;
while ((Line = Reader.ReadLine()) != null)
{
TemplateBody.Append(Line);
}
}
return TemplateBody.ToString();
}
}