1

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.enter image description here

3
  • You've added a tbody for each item in the list!. Remove the tbody from within the loop and add it to your template. You may also want to remove the extra spaces from your HTML tags < tr should be <tr - no space Commented May 14, 2021 at 5:25
  • You could also use a template engine like github.com/StubbleOrg/Stubble / github.com/wildbit/mustachio to define how item data repeats within the template file instead of being hard coded. Commented May 14, 2021 at 6:28
  • Thank you guys. I already remove the tbody on and the spaces between tags. Commented May 14, 2021 at 7:11

1 Answer 1

1

You are adding multiple <tbody> elements to your table but that isn't the main problem. You need to remove the extra spaces from your < tr and < td elements. See snippet below for example with and without spaces.

You should also use StringBuilder when manipulating strings like this as it's more efficient.

var sb = new StringBuilder();
foreach (var itemList in orderProducts)
{       
  sb.AppendLine("<tbody style=\"border: 0; border-bottom: 1px dashed #ccc;\">");
  sb.AppendLine("<tr>");
  sb.AppendLine("<td style=\"text-align:center;\" width=\"200\">{Image}</td>");
  sb.AppendLine("<td style=\"text-align:left\" width=\"250\">"+ itemList.Name + "</td>");
  sb.AppendLine("<td style=\"text-align:center\" width=\"100\">" + itemList.Quantity + "</td>");
  sb.AppendLine("<td style=\"text-align:center\" width=\"100\">" + itemList.Price +"</td>");
  sb.AppendLine("</tr>");
  sb.AppendLine("</tbody>");
}

var items = sb.ToString();

<h1>Without Spaces</h1>

<table cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;">
 <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>
</table>

<h1>With Spaces</h1>

<table cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;">
 < 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>
</ table>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.