【问题标题】:SMTP mail : How to bind data table values to a html template C#SMTP 邮件:如何将数据表值绑定到 html 模板 C#
【发布时间】:2018-12-19 08:33:41
【问题描述】:

我是 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 邮件发送。谁能帮我解决这个问题。

【问题讨论】:

    标签: c# .net api asp.net-web-api smtp


    【解决方案1】:

    如果您有一堆不同的模板,我建议您选择高级解决方案,例如 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
    

    【讨论】:

    • 如何在第三部分中包含 tableRowTemplate 和 tableBodyTemplate @Imantas Sankalas
    • 最简单但最丑陋的方法就是将它们硬编码为字符串。但是,我建议将它们移动到文件中并执行 Read var mailBodyTemplate = File.ReadAllText("path/to/mailBody.html");
    • 我可以使用上面的 html 模板显示图像。你能帮我吗?@Imantas stackoverflow.com/questions/54002475/…
    • 我是新手,就我而言,我该如何实现这个概念@Imantas
    猜你喜欢
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2012-07-09
    • 2012-03-11
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2012-01-22
    相关资源
    最近更新 更多