如何使用电子邮件模板发送电子邮件 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10512845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:09:03  来源:igfitidea点击:

how to send email wth email template c#

c#email-templates

提问by Thomas

suppose i need to send mail to customer with customer detail and his order detail. i have template html data in a html file.customer data is there and as well as order detail is also there in same html template file. my html look like

假设我需要将带有客户详细信息和他的订单详细信息的邮件发送给客户。我在 html 文件中有模板 html 数据。客户数据在那里,订单详细信息也在同一个 html 模板文件中。我的 html 看起来像

<html>
<body>
Hi {FirstName} {LastName},

Here are your orders: 
{foreach Orders}
    Order ID {OrderID} Quantity : {Qty} <strong>{Price}</strong>. 
{end}

</body>
</html>

now i want to fill up all sample keyword surrounded with {} with actual value and also iterate and fill up orders.

现在我想用实际值填充所有用 {} 包围的示例关键字,并迭代和填充订单。

i search google and found that microsoft provide a class called MailDefinitionby which we can generate mail body dynamically. i got a sample code also like

我搜索谷歌,发现微软提供了一个名为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());

by the above code we can replace pseudo value with actual value but i don't know how iterate in Orders detail and populate orders data.

通过上面的代码,我们可以用实际值替换伪值,但我不知道如何在订单详细信息中迭代并填充订单数据。

so if it is possible using MailDefinition class then please guide me with code that how can i iterate in loop and generate body for orders detail.

因此,如果可以使用 MailDefinition 类,那么请指导我如何在循环中迭代并为订单详细信息生成正文的代码。

采纳答案by David Tischler

As an alternative to MailDefinition, have a look at RazorEngine https://github.com/Antaris/RazorEngine.

作为 MailDefinition 的替代方案,请查看 RazorEngine https://github.com/Antaris/RazorEngine

RazorEngine is a simplified templating framework built around Microsoft's new Razor parsing engine, used in both ASP.NET MVC3 and Web Pages. RazorEngine provides a wrapper and additional services built around the parsing engine to allow the parsing technology to be used in other project types.

RazorEngine 是围绕 Microsoft 新的 Razor 解析引擎构建的简化模板框架,用于 ASP.NET MVC3 和网页。RazorEngine 提供了围绕解析引擎构建的包装器和附加服务,以允许在其他项目类型中使用解析技术

It lets you use razor templates outside of ASP.NET MVC and then write something like this (not tested):

它允许您在 ASP.NET MVC 之外使用 razor 模板,然后编写如下内容(未测试):

string template =
@"<html>
<body>
Hi @Model.FirstName @Model.LastName,

Here are your orders: 
@foreach(var order in Model.Orders) {
    Order ID @order.Id Quantity : @order.Qty <strong>@order.Price</strong>. 
}

</body>
</html>";

var model = new OrderModel {
    FirstName = "Martin",
    LastName = "Whatever",
    Orders = new [] {
        new Order { Id = 1, Qty = 5, Price = 29.99 },
        new Order { Id = 2, Qty = 1, Price = 9.99 }
    }
};

string mailBody = Razor.Parse(template, model);

回答by Mario

You can't do such "complicated" logic with the default replacement stuff (the placeholder handling is made to be used for simple variables only, e.g. names or values).

您不能使用默认替换内容执行这种“复杂”逻辑(占位符处理仅用于简单变量,例如名称或值)。

You'll have to do the parsing yourself. Depending on the complexity (e.g. loops withing loops), this can get a bit tricky.

您必须自己进行解析。根据复杂性(例如带有循环的循环),这可能会有点棘手。

If you don't want or need such things, it's more trivial. E.g. use the regular expression \{foreach (.*?)\}(.*?)\{end\}to find such loops, then parse the contents/matched groups the way you need. Once that part is done, you could replace other values or use the default replacement feature.

如果你不想要或不需要这样的东西,那就更简单了。例如,使用正则表达式\{foreach (.*?)\}(.*?)\{end\}查找此类循环,然后按照您需要的方式解析内容/匹配组。完成该部分后,您可以替换其他值或使用默认替换功能。

Only downside with this approach is the fact that you'll have to recreate the mail for each recipient (i.e. you can't mass mail using MailDefinition).

这种方法的唯一缺点是您必须为每个收件人重新创建邮件(即您不能使用 群发邮件MailDefinition)。