你可以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 />
在所有如果客戶選擇任何選項出現,那麼你可以做到這一點(雖然有點哈克): -
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);
確保與{1}
替換此行的標記
<strong>You chose option: </strong>{1}<br />
請顯示代碼的一些細節。 – Hogan 2013-04-11 10:24:03
使用'StringBuilder'在代碼後面生成你的HTML正文:) – 2013-04-11 10:26:20
謝謝'StringBuilder'的想法:) – intentarr 2013-04-11 10:38:50