嗨,您可以使用 mailMessage 类将 .htm 文件作为 html 模板发送吗?我似乎不知道该怎么做?
问问题
1280 次
2 回答
1
Assuming you have created an html template that has markers (e.g. [[[FROM]]]
), you then need to use StringBuilder. For example:
public static string GetEmailTemplateContent(string path)
{
string emailHtmlTemplate = "";
StreamReader rdr = null;
try
{
rdr = new StreamReader(path);
emailHtmlTemplate = rdr.ReadToEnd();
}
catch (IOException ex)
{
throw new Exception(ex.Message.ToString());
}
finally
{
if (rdr != null)
{
rdr.Close();
rdr.Dispose();
}
}
return emailHtmlTemplate;
}
And then, you just need to replace the markers and fill the email body to the changed template.
string emailTemplate = GetEmailTemplateContent("C:\\somepath");
emailTemplate = emailTemplate.Replace("[[[FROM]]]", from);
MailMessage email = new MailMessage();
email.Body = emailText;
于 2012-09-05T13:50:23.427 回答
0
我建议您使用 String 或 StringBuilder 类并制作您自己的模板子系统。我的意思是,只需将模板内容分配给字符串,并用实际值替换出现的标记。
如果您想要一个示例,请查看 Drupal 如何提供像 !username 等变量。如果您需要代码示例,请不要犹豫在此处发表评论。
希望这可以帮助,
于 2010-08-27T16:58:51.493 回答