SMTP 邮件:如何将数据表值绑定到 html 模板 C#

我是 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


我在 SO 中进行了搜索,但我只能找到将字符串作为 SMTP 邮件发送。任何人都可以帮我解决这个问题。


米琪卡哇伊
浏览 123回答 1
1回答

MM们

如果您有一堆不同的模板,我建议您选择像StringTemplate或RazorEngine这样的高级解决方案,但如果这是您需要涵盖的唯一情况,您可以做一些简单的事情,将您的 HTML 模板分成两部分 - 邮件正文和表格行,构建您的表格行集合并将其注入邮件正文。邮件正文<!DOCTYPE html><html><head>&nbsp; &nbsp; <title></title>&nbsp; &nbsp; <meta charset="utf-8" /></head><body>&nbsp; &nbsp; <h4 style="color:&nbsp; &nbsp;#00CED1">Purchase Details<hr></h4>&nbsp; &nbsp; <table style="width:100%;border: 1px solid black;">&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Item(s)<hr></th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Details<hr></th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Amount(Tax.Inc)<hr></th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; {0}&nbsp; &nbsp; </table>&nbsp; &nbsp; <hr>&nbsp; &nbsp; <table style="width:100%;">&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td align="right" style="font-size:22px;font-weight:bold;">Total Amount :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td align="right" style="color:#0000CD;font-size:22px;font-weight:bold;">{1}</td></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </table>&nbsp; &nbsp; <hr></body></html>表行<tr>&nbsp; &nbsp; <td align="center"><img src="https://dummyimage.com/80x80/000/fff" /></td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <table style="width:100%;margin-left:15px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td align="left">Product Name :</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td align="left" style="color:#32CD32;font-weight:bold;">{0}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td align="left">Product UOM :</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td align="left" style="color:#696969;font-weight:bold;">{1}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td align="left">Quantity :</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td align="left" style="color:#778899;font-weight:bold;">{2}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td align="left">Unit Price :</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td align="left" style="color:#483D8B;font-weight:bold;">{3}<td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; </td>&nbsp; &nbsp; <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){&nbsp; &nbsp; totalPrice += Convert.ToInt32(Row["Price"]);&nbsp; &nbsp; 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
打开App,查看更多内容
随时随地看视频慕课网APP