|
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Web;
using JetBrains.Annotations;
namespace Kaleida.ServiceMonitor.Model.Email
{
class HtmlMessageBuilder
{
private readonly StringBuilder content = new StringBuilder();
public void AddLine(IHtmlString text)
{
content.Append("<p>");
content.Append(text);
content.Append("</p>");
content.AppendLine();
}
[StringFormatMethod("format")]
public void AddRawFormat(string format, params object[] args)
{
AddRaw(string.Format(format, args));
}
public void AddRaw(string text)
{
content.Append(text.AsRawHtml());
}
public void AddHeading(IHtmlString text)
{
content.AppendLine();
content.Append("<h1>");
content.Append(text);
content.Append("</h1>");
content.AppendLine();
}
public void AddBullets(IEnumerable<IHtmlString> bulletLines)
{
content.Append("<ul>\r\n");
foreach(var line in bulletLines)
{
content.AppendFormat(" <li>{0}</li>\r\n", line);
}
content.Append("</ul>\r\n");
}
public void AddTable(BasicTable table)
{
content.Append("<table>\r\n");
content.Append("<tr>\r\n");
AddRaw(string.Join("\r\n", table.Headings.Select(i => "<th>" + i + "</th>")));
content.Append("</tr>\r\n");
foreach(var row in table.Rows)
{
content.Append("<tr>\r\n");
AddRaw(string.Join("\r\n", row.Items.Select(i => "<td>" + i + "</td>")));
content.Append("</tr>\r\n");
}
content.Append("</table>\r\n");
}
public string GetContent()
{
var sb = new StringBuilder();
sb.AppendLine("<html>");
sb.AppendLine("<head>");
sb.AppendLine("<style type='text/css'>");
sb.AppendLine("body {font-family: sans-serif; font-size:10pt;}");
sb.AppendLine("table {border: 1px solid black; border-collapse: collapse; font-size:9pt;}");
sb.AppendLine("h1 {font-size:12pt;}");
sb.AppendLine("td {border: 1px solid black; padding: 1px 2px 1px 2px;}");
sb.AppendLine("th {border: 1px solid black; padding: 1px 2px 1px 2px;}");
sb.AppendLine("</style>");
sb.AppendLine("</head>");
sb.AppendLine("<body>");
sb.Append(content.ToString());
sb.AppendLine("</body>");
sb.AppendLine("</html>");
return sb.ToString();
}
public override string ToString()
{
return GetContent();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Stuart Wheelwright is the Principal Architect and Software Developer at
BlackJet Software Ltd.
He has over 16 years commercial experience producing robust, maintainable, web-based solutions and bespoke systems for Microsoft platforms.
His latest project is
Shopping UK, an elegantly simple shopping list for iPhone.