我是c#和WEB Api的新手,我想在每次购买产品后发送邮件。 邮件将包含与所购买产品有关的所有详细信息。
在这里,我的代码在将字符串数据作为邮件发送时工作正常。
我不知道如何将数据表值添加到HTML模板并将其作为邮件发送。
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>
<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;">Red chilly powder</td>
</tr>
<tr>
<td align="left">Product UOM :</td>
<td align="left" style="color:#696969;font-weight:bold;">PKT 500</td>
</tr>
<tr>
<td align="left">Quantity :</td>
<td align="left" style="color:#778899;font-weight:bold;">5</td>
</tr>
<tr>
<td align="left">Unit Price :</td>
<td align="left" style="color:#483D8B;font-weight:bold;">30 $
<td>
</tr>
</table>
</td>
<td align="center" style="font-size:20px;font-weight:bold;color:#1E90FF">150 $</td>
</tr>
</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;">150 $</td></td>
</tr>
</table>
<hr>
</body>
</html>
API
foreach (DataRow Row in Tables[0].Rows)
{
//Here I want to iterate the table rows.
}
sendMail(string mMailBody);
我已经搜索过SO,但是我只能找到以SMTP邮件形式发送字符串。有人可以帮助我解决这个问题。
答案 0 :(得分:0)
如果您有一堆不同的模板,建议您选择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