您可以将 html 存储在 XML 文件中并通过string.Format
如下方式填充内容:-
<?xml version="1.0" encoding="utf-8" ?>
<Email>
<FromAddress>from</FromAddress>
<ToAddress>to</ToAddress>
<Subject>subject line</Subject>
<EmailBody>
<![CDATA[
<html>
<head>
<title>Customer</title>
</head>
<div valign="top">
<font color="#666666" face="Arial, Helvetica, sans-serif, Verdana" size="2">
<p>Hello user.</p>
<p><strong>This is your ID in the system: </strong>{0}<br />
<strong>You chose option: </strong>{1}<br /></p>
</font>
</div>
</html>
]]>
</EmailBody>
</Email>
代码(填充和发送): -
int custId = //provide customer id
string option = //customers selected option
string custEmail = //customers email
MailMessage mail = GetHtmlEmail();
string message = string.Format(mail.Body, custId, option);
mail.IsBodyHtml = true;
mail.Body = message;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Send(mail);
}
读取电子邮件标记 + 设置邮件对象的一些属性:-
private MailMessage GetHtmlEmail()
{
MailMessage mail = new MailMessage();
XmlTextReader xReader = new XmlTextReader(Server.MapPath("PATH TO EMAIL.XML"));
while (xReader.Read())
{
switch (xReader.Name)
{
case "ToAddress":
mail.To.Add(xReader.ReadElementContentAs(typeof(string), null).ToString());
break;
case "FromAddress":
mail.From = new MailAddress(xReader.ReadElementContentAs(typeof(string), null).ToString());
break;
case "Subject":
mail.Subject = xReader.ReadElementContentAs(typeof(string), null).ToString();
break;
case "EmailBody":
mail.Body = xReader.ReadElementContentAs(typeof(string), null).ToString();
break;
default:
break;
}
}
return mail;
}
编辑*如果您不希望<strong>You chose option: </strong>{1}<br />
在客户没有选择任何选项的情况下出现,那么您可以这样做(虽然有点hacky):-
if(!string.IsNullOrEmpty(option))
{
option = string.Format("<strong>You chose option: </strong>{1}<br />", option);
}
else
{
option = string.Empty;
}
然后像往常一样传递它: -
string message = string.Format(mail.Body, custId, option);
确保将标记中的这一行替换<strong>You chose option: </strong>{1}<br />
为{1}