Hi
I have below code & displaying like normal string in Body of Email
C#
StringBuilder htmlTable = new StringBuilder();
//htmlTable.Append("");
htmlTable.Append("");
htmlTable.Append("table { border-collapse: collapse; width: 100%; }");
htmlTable.Append("th, td { border: 1px solid black; padding: 8px; text-align: left; }");
htmlTable.Append("th { background-color: #f2f2f2; }");
htmlTable.Append("");
htmlTable.Append("");
// Add headers to the table
htmlTable.Append("");
htmlTable.Append("Transaction ID");
htmlTable.Append("Doc. No");
htmlTable.Append("Doc. Date");
htmlTable.Append("");
while (!oRecordSet.EoF)
{
htmlTable.Append("");
htmlTable.Append("" + oRecordSet.Fields.Item("TransId").Value.ToString() + "");
htmlTable.Append("" + oRecordSet.Fields.Item("DocNum").Value.ToString() + "");
htmlTable.Append("" + oRecordSet.Fields.Item("DocDate").Value.ToString() + "");
htmlTable.Append("");
oRecordSet.MoveNext();
}
htmlTable.Append("");
string body = htmlTable.ToString();
*************************************************************************
table { border-collapse: collapse; width: 100%; }th, td { border: 1px solid black; padding: 8px; text-align: left; }th { background-color: #f2f2f2; }Transaction IDDoc. NoDoc. Date165506951072141/8/2025 12:00:00 AM165591251072221/8/2025 12:00:00 AM21097147000725/11/2024 12:00:00 AM21202247000735/11/2024 12:00:00 AM825928100458640000729/6/2024 12:00:00 AM113453040000789/27/2024 12:00:00 AMThanks

Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 7, 2025, 2:01 AM EST
Hello Ramco,
The issue you're facing is likely due to the email client not rendering the HTML correctly or the
IsBodyHtmlproperty not being set totruein theMailMessageobject. WithoutIsBodyHtml = true, the email client will treat the content as plain text, and the HTML tags will be displayed as a normal string instead of being rendered as a table.Here’s how you can fix the issue and ensure the HTML table is displayed correctly in the email body:
// Build the HTML table StringBuilder htmlTable = new StringBuilder(); htmlTable.Append(""); htmlTable.Append("table { border-collapse: collapse; width: 100%; }"); htmlTable.Append("th, td { border: 1px solid black; padding: 8px; text-align: left; }"); htmlTable.Append("th { background-color: #f2f2f2; }"); htmlTable.Append(""); htmlTable.Append(""); // Add headers to the table htmlTable.Append(""); htmlTable.Append("Transaction ID"); htmlTable.Append("Doc. No"); htmlTable.Append("Doc. Date"); htmlTable.Append(""); // Add rows to the table while (!oRecordSet.EoF) { htmlTable.Append(""); htmlTable.Append("" + oRecordSet.Fields.Item("TransId").Value.ToString() + ""); htmlTable.Append("" + oRecordSet.Fields.Item("DocNum").Value.ToString() + ""); htmlTable.Append("" + oRecordSet.Fields.Item("DocDate").Value.ToString() + ""); htmlTable.Append(""); oRecordSet.MoveNext(); } htmlTable.Append(""); // Convert the StringBuilder to a string string body = htmlTable.ToString(); // Email configuration var addresses = "[email protected];[email protected]"; var fromAddress = new MailAddress("[email protected]", "Sender"); const string fromPassword = "Password"; // Use app-specific password if 2FA is enabled string subject = "[Message] Data in HTML Table"; var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; // Send the email foreach (var address in addresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) { using (var message = new MailMessage(fromAddress, new MailAddress(address)) { Subject = subject, Body = body, IsBodyHtml = true // Ensure this is set to true for HTML content }) { try { smtp.Send(message); Console.WriteLine($"Email sent to {address}"); } catch (Exception ex) { Console.WriteLine($"Failed to send email to {address}: {ex.Message}"); } } }