I'm working on sending an email for order confirmation and I need to loop every items that the user order from my website. So, I use the foreach to load very items in the email but it return a html code.
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath(templatePath)))
{
body = reader.ReadToEnd();
}
var items = "";
foreach (var itemList in orderProducts)
{
items += "<tbody style=\"border: 0; border - bottom: 1px dashed #ccc;\"><tr>< td style = \"text-align:center;\" width = \"200\" >{ Image}</ td >< td style = \"text-align:left\" width = \"250\" >"+ itemList.Name + "</ td >< td style = \" text-align:center\" width = \"100\" >" + itemList.Quantity + "</ td >< td style = \" text-align:center\" width = \"100\" >" + itemList.Price +"</ td ></ tr ></ tbody > ";
}
body = body.Replace("{Items}", items);
System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(body, null, "text/html");
mailMessage.AlternateViews.Add(htmlView);
//Send message
System.Net.Mail.SmtpClient smtpClient = new
System.Net.Mail.SmtpClient(mailServer);
smtpClient.Port = 587;
smtpClient.Credentials = new
System.Net.NetworkCredential(senderMail,senderPassword);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
my email template
<table cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;margin-top:-20px;">
<thead style="border: 0;border-bottom: 1px dashed #ccc;">
<tr>
<td style=" text-align:center" width="200"><b>ITEMS ORDERED</b></td>
<td style=" text-align:center" width="250">NAME</td>
<td style=" text-align:center" width="100">QTY</td>
<td style=" text-align:center" width="100">PRICE</td>
</tr>
</thead>
{Items}
</table>
and here's the result in my email. How to read the html code? Thank you for helping me.
tbodyfor each item in the list!. Remove thetbodyfrom within the loop and add it to your template. You may also want to remove the extra spaces from your HTML tags< trshould be<tr- no space