1 回答
TA贡献1886条经验 获得超2个赞
如果您有一堆不同的模板,我建议您选择像StringTemplate或RazorEngine这样的高级解决方案,但如果这是您需要涵盖的唯一情况,您可以做一些简单的事情,将您的 HTML 模板分成两部分 - 邮件正文和表格行,构建您的表格行集合并将其注入邮件正文。
邮件正文
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<h4 style="color: #00CED1">Purchase Details<hr></h4>
<table style="width:100%;border: 1px solid black;">
<tr>
<th>Item(s)<hr></th>
<th>Details<hr></th>
<th>Amount(Tax.Inc)<hr></th>
</tr>
{0}
</table>
<hr>
<table style="width:100%;">
<tr>
<td align="right" style="font-size:22px;font-weight:bold;">Total Amount :
<td align="right" style="color:#0000CD;font-size:22px;font-weight:bold;">{1}</td></td>
</tr>
</table>
<hr>
</body>
</html>
表行
<tr>
<td align="center"><img src="https://dummyimage.com/80x80/000/fff" /></td>
<td>
<table style="width:100%;margin-left:15px">
<tr>
<td align="left">Product Name :</td>
<td align="left" style="color:#32CD32;font-weight:bold;">{0}</td>
</tr>
<tr>
<td align="left">Product UOM :</td>
<td align="left" style="color:#696969;font-weight:bold;">{1}</td>
</tr>
<tr>
<td align="left">Quantity :</td>
<td align="left" style="color:#778899;font-weight:bold;">{2}</td>
</tr>
<tr>
<td align="left">Unit Price :</td>
<td align="left" style="color:#483D8B;font-weight:bold;">{3}<td>
</tr>
</table>
</td>
<td align="center" style="font-size:20px;font-weight:bold;color:#1E90FF">{4}</td>
</tr>
然后代码
// Retrieve the templates and store them into mailBodyTemplate and tableRowTemplate
// For example,
var mailBodyTemplate = File.ReadAllText("mailBody.html");
var tableRowTemplate = File.ReadAllText("tableRow.html");
var tableRows = new StringBuilder();
var totalPrice = 0;
foreach (DataRow Row in Tables[0].Rows)
{
totalPrice += Convert.ToInt32(Row["Price"]);
tableRows.AppendFormat(tableRowTemplate, Row["Name"], Row["UOM"], Row["Quantity"], Row["UnitPrice"], Row["Price"]);
}
var mailBody = string.Format(mailBodyTemplate, tableRows.ToString(), totalPrice);
// Send your mail body
- 1 回答
- 0 关注
- 122 浏览
添加回答
举报