Yas Smitch
Yas Smitch

Reputation: 279

How to pass HTML code to email in ASP.Net MVC C#

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

Upvotes: 1

Views: 989

Answers (1)

phuzi
phuzi

Reputation: 13060

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>

Upvotes: 1

Related Questions