Mattaceten Mattaceten - 28 days ago 6
C# Question

How to format HTML table properly in Outlook mailitem

I'm using C# to pragmatically create an outlook mail item that populates an html table. The problem is, when the email is sent it looks like hell on earth in outlook. I understand everything formats differently in outlook versus chrome. I understand that html elements need to contain inline styles, and that i cannot use CSS. Knowing all of this and creating a nice inline table in Brackets IDE and then using the same code in C# and sending an email to outlook, it still looks terrible.

How can I accomplish a decent formatted table using HTML/outlook??

Here is some of my C# code.

sb.Append("<table border=\"1px solid black\";>");
sb.Append("<tr>");
sb.AppendFormat("<th style=\"1px solid black\";>{0}</th>");
sb.AppendFormat("<th style=\"1px solid black\";>{0}</th>");
sb.AppendFormat("<th style=\"1px solid black\";>{0}</th>");
sb.AppendFormat("<th style=\"1px solid black\";>{0}</th>");
sb.AppendFormat("<th style=\"1px solid black\";>{0}</th>");
sb.AppendFormat("<th style=\"1px solid black\";>{0}</th>");
sb.AppendFormat("<th style=\"1px solid black\";>{0}</th>");
sb.AppendFormat("<th style=\"1px solid black\";>{0}</th>");
sb.Append("</tr>");
sb.Append("</table>");

Answer

1) If you target Outlook mail client only, I would suggest to create table in one of the Microsoft tools - either in Outlook itself or Word. In Outlook you have to view raw email to get email markup, for Word you can save document as HTML.

2) In your example code it seems that 'border' part is missing in your style attributes.

sb.AppendFormat("<th style=\"border: 1px solid black\";>{0}</th>");

I guess it works in browser because browsers tolerate badly formatted markup pretty good, while Outlook not so much.