假设我需要发送邮件给客户与客户的细节和他的订单细节。 我有一个html文件中的模板html数据。客户数据在那里,以及订单的细节也在同一个html模板文件中。我的HTML看起来像如何发送电子邮件与电子邮件模板c#
<html>
<body>
Hi {FirstName} {LastName},
Here are your orders:
{foreach Orders}
Order ID {OrderID} Quantity : {Qty} <strong>{Price}</strong>.
{end}
</body>
</html>
现在我想填补包围{}与实际值的所有样品的关键字,并进行迭代,并填写订单。
我搜索谷歌,发现微软提供了一个名为MailDefinition 我们可以动态生成邮件正文。我有一个示例代码也由上面的代码,我们可以用实际值替换伪值喜欢
MailDefinition md = new MailDefinition();
md.From = "[email protected]";
md.IsBodyHtml = true;
md.Subject = "Test of MailDefinition";
ListDictionary replacements = new ListDictionary();
replacements.Add("<%Name%>", "Martin");
replacements.Add("<%Country%>", "Denmark");
string body = "
Hello <%Name%> You're from <%Country%>.";
MailMessage msg = md.CreateMailMessage("[email protected]", replacements, body, new System.Web.UI.Control());
,但我不知道如何在订单详细迭代和填充订单数据。
所以如果有可能使用MailDefinition类,那么请用代码指导我如何迭代循环并生成订单细节的正文。
如果第三方库是一个选项,看看[dotLiquid](http://dotliquidmarkup.org/)。你也可能想看到我的答案类似的问题:http://stackoverflow.com/a/28558160/79485 – Marcel
我如何下载*** MailDefinition ***? – Kiquenet
MailDefinition是.NET中System.Web名称空间的一部分。你不需要下载它。 –