Search code examples
c#.netasp.net-web-apismtp

SMTP mail : How to bind data table values to a html template C#


I am new to C# and WEB API,I want to send mail after each purchase of a product. Mail will have all the details related to the purchased product.

Here I have the code which is working fine while sending string data as a mail.

I don't know how to add data table values to the HTML template and send it as a mail.

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);

I have searched in Stack Overflow but I can find only sending string as a SMTP mail. Can anyone help me to solve this?


Solution

  • If you have a bunch of different templates I'd suggest choosing advanced solution like StringTemplate or RazorEngine, but if it's the only case you need to cover, you could do something simple as separating your HTML template in two parts - mail body and table row, build your table row collection and inject it into the mail body.

    Mail Body

    <!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>
    

    Table Row

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

    Then the code

    // 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