/// <summary> /// Gets the default email table template. /// </summary> /// <returns>EmailTableTemplate.</returns> public static EmailTableTemplate GetDefaultTemplate() { var template = new EmailTableTemplate { Name = "Default", HtmlHeaderBackgroundColor = "FF0000", HtmlHeaderFontColor = "white", UseAlternateRowColors = false }; return(template); }
/// <summary> /// Gets the email table templates. /// </summary> /// <returns>List<EmailTableTemplate>.</returns> public static List <EmailTableTemplate> GeTableTemplates() { var templates = new List <EmailTableTemplate>(); string templateText = File.ReadAllText(@"TableTemplates.json"); var results = JObject.Parse(templateText); foreach (var template in results["tabletemplates"]) { var currTemplate = new EmailTableTemplate { Name = (string)template["Name"], AlternateRowColor = (string)template["AlternateRowColor"], HtmlHeaderBackgroundColor = (string)template["HtmlHeaderBackgroundColor"], HtmlHeaderFontColor = (string)template["HtmlHeaderFontColor"], UseAlternateRowColors = Convert.ToBoolean(template["UseAlternateRowColors"]) }; if (template["CssTableStyle"] != null) { JArray styleArray = JArray.Parse(template["CssTableStyle"].ToString()); StringBuilder sb = new StringBuilder(); foreach (var line in styleArray) { sb.AppendLine(line.ToString()); } string stageCss = sb.ToString(); stageCss = stageCss.Replace("\r\n", string.Empty).Replace(@"\", string.Empty); currTemplate.CssTableStyle = stageCss; } templates.Add(currTemplate); } templates.Add(GetDefaultTemplate()); return(templates); }
/// <summary> /// Creates the HTML content for the email. /// </summary> /// <param name="testedAudit">The tested audit.</param> /// <param name="testData">The test data.</param> /// <param name="emailTableTemplate">The email table template.</param> /// <returns>System.String.</returns> public static string CreateHtmlData(Audit testedAudit, DataSet testData, EmailTableTemplate emailTableTemplate) { var sb = new StringBuilder(); if (emailTableTemplate.Equals(null)) { emailTableTemplate = GetDefaultTemplate(); } int tableNamesCount = 0; foreach (DataTable currTable in testData.Tables) { if (testedAudit.Test.MultipleResults) { sb.Append("<B>"); sb.Append(testedAudit.Test.TableNames[tableNamesCount]); sb.Append("</B>"); sb.AppendLine("<br>"); } sb.AppendFormat(@"<caption> Total Rows = "); sb.AppendFormat(currTable.Rows.Count.ToString(CultureInfo.InvariantCulture)); sb.AppendFormat(@"</caption>"); if (!string.IsNullOrEmpty(emailTableTemplate.CssTableStyle)) { sb.AppendLine("<style>"); sb.AppendLine(emailTableTemplate.CssTableStyle); sb.AppendLine("</style>"); sb.Append("<TABLE id=emailtable>"); sb.Append("<TR>"); } else { sb.Append("<TABLE BORDER=1>"); sb.Append("<TR ALIGN='LEFT' style='white-space: nowrap;'>"); } // first append the column names. foreach (DataColumn column in currTable.Columns) { if (!string.IsNullOrEmpty(emailTableTemplate.CssTableStyle)) { sb.Append("<TH>" + column.ColumnName + "</TH>"); } else { sb.Append("<TD style='white-space: nowrap;' bgcolor=\"" + emailTableTemplate.HtmlHeaderBackgroundColor + "\"><B>"); sb.Append("<font color=\"" + emailTableTemplate.HtmlHeaderFontColor + "\">" + column.ColumnName + "</font>"); sb.Append("</B></TD>"); } } sb.Append("</TR>"); int rowCounter = 1; // next, the column values. foreach (DataRow row in currTable.Rows) { if (!string.IsNullOrEmpty(emailTableTemplate.CssTableStyle)) { if (emailTableTemplate.UseAlternateRowColors) { if (rowCounter % 2 == 0) { // Even numbered row, so tag it with a different background color. sb.Append("<TR style='white-space: nowrap;' ALIGN='LEFT' bgcolor=\"" + emailTableTemplate.AlternateRowColor + "\">"); } else { sb.Append("<TR style='white-space: nowrap;' ALIGN='LEFT'>"); } } else { sb.Append("<TR>"); } } else { if (emailTableTemplate.UseAlternateRowColors) { if (rowCounter % 2 == 0) { // Even numbered row, so tag it with a different background color. sb.Append("<TR style='white-space: nowrap;' ALIGN='LEFT' bgcolor=\"" + emailTableTemplate.AlternateRowColor + "\">"); } else { sb.Append("<TR style='white-space: nowrap;' ALIGN='LEFT'>"); } } else { sb.Append("<TR style='white-space: nowrap;' ALIGN='LEFT'>"); } } foreach (DataColumn column in currTable.Columns) { if (!string.IsNullOrEmpty(emailTableTemplate.CssTableStyle)) { sb.Append("<TD>"); if (row[column].ToString().Trim().Length > 0) { sb.Append(row[column]); } else { sb.Append(" "); } } else { sb.Append("<TD style='white-space: nowrap;'>"); if (row[column].ToString().Trim().Length > 0) { sb.Append(row[column]); } else { sb.Append(" "); } } sb.Append("</TD>"); } sb.Append("</TR>"); rowCounter++; } sb.Append("</TABLE>"); sb.Append("<br>"); tableNamesCount++; } return(sb.ToString()); }
private static string PrepareResultsSingleAudit(Audit testedAudit, DataSet testData) { var body = new StringBuilder(); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); string sourceEmailDescription = config.AppSettings.Settings["sourceEmailDescription"].Value; if (testedAudit.Test.SendReport) { testedAudit.ShowThresholdMessage = false; testedAudit.ShowQueryMessage = false; if (testedAudit.EmailSubject != null) { body.AppendLine("<h2>" + testedAudit.EmailSubject + "</h2>"); } body.Append("This report ran at " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt", CultureInfo.InvariantCulture) + AuditUtils.HtmlBreak + AuditUtils.HtmlBreak); } if (testedAudit.ShowThresholdMessage) { body.AppendLine("<h2>ERROR MESSAGE</h2>"); body.Append(testedAudit.Test.FailedMessage + AuditUtils.HtmlBreak + AuditUtils.HtmlBreak); } if (testedAudit.ShowCommentMessage) { body.AppendLine("COMMENTS AND INSTRUCTIONS" + AuditUtils.HtmlBreak); body.AppendLine("============================" + AuditUtils.HtmlBreak); if (testedAudit.Test.Instructions != null) { if (testedAudit.Test.Instructions.Length > 0) { body.Append(testedAudit.Test.Instructions.ToHtml() + AuditUtils.HtmlBreak); body.AppendLine(AuditUtils.HtmlBreak); } } } if (testedAudit.IncludeDataInEmail) { if (testData.Tables.Count > 0) { EmailTableTemplate currTemplate = testedAudit.Test.TemplateColorScheme; string htmlData = AuditUtils.CreateHtmlData(testedAudit, testData, currTemplate); body.Append(htmlData); } } body.AppendLine(AuditUtils.HtmlBreak); if (testedAudit.Test.SendReport) { body.Append("This report ran at " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt", CultureInfo.InvariantCulture) + AuditUtils.HtmlBreak); body.Append("<b>This report was run on: " + testedAudit.TestServer + "</b>" + AuditUtils.HtmlBreak + AuditUtils.HtmlBreak); } else { body.Append("This audit ran at " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt", CultureInfo.InvariantCulture) + AuditUtils.HtmlBreak); } if (testedAudit.ShowQueryMessage) { body.Append(AuditUtils.HtmlBreak); body.Append("The '" + testedAudit.Name + "' audit has failed. The following SQL statement was used to test this audit :" + AuditUtils.HtmlBreak); body.Append(testedAudit.Test.SqlStatementToCheck.ToHtml() + AuditUtils.HtmlBreak); body.Append("<b>This query was run on: " + testedAudit.TestServer + "</b>" + AuditUtils.HtmlBreak + AuditUtils.HtmlBreak); } string cleanBody = body.ToString().Replace("\r\n", string.Empty); return(cleanBody); }