4

我正在尝试在电子邮件正文中显示数据表。我正在使用以下函数在 html 标签中转换数据表

private static string getHTML(DataTable dt)

    {
        StringBuilder myBuilder = new StringBuilder();

        myBuilder.Append("<table border='1px' cellpadding='5' cellspacing='0' ");
        myBuilder.Append("style='border: solid 1px Silver; font-size: x-small;'>");

        myBuilder.Append("<tr align='left' valign='top'>");
        foreach (DataColumn myColumn in dt.Columns)
        {
            myBuilder.Append("<td align='left' valign='top'>");
            myBuilder.Append(myColumn.ColumnName);
            myBuilder.Append("</td>");
        }
        myBuilder.Append("</tr>");

        foreach (DataRow myRow in dt.Rows)
        {
            myBuilder.Append("<tr align='left' valign='top'>");
            foreach (DataColumn myColumn in dt.Columns)
            {
                myBuilder.Append("<td align='left' valign='top'>");
                myBuilder.Append(myRow[myColumn.ColumnName].ToString());
                myBuilder.Append("</td>");
            }
            myBuilder.Append("</tr>");
        }
        myBuilder.Append("</table>");

        return myBuilder.ToString();
    }

并将返回的字符串提供给 stringbuilder,如下所示

StringBuilder sb = new StringBuilder(); 
                sb.AppendLine("Dear Responsible,"); 
                sb.AppendLine(""); 
                sb.AppendLine("Following items are added."); 
                sb.AppendLine(""); 
                sb.AppendLine(myBuilder.ToString())    //here I want the data to       display in table format
                sb.AppendLine("Many thanks in advance!");
                sb.AppendLine("");

但我的邮件显示如下

Dear Responsible,

Following items are added.

<table border='1px' cellpadding='5' cellspacing='0' style='border: solid 1px Silver; font-size: x-small;'><tr align='left' valign='top'><td align='left' valign='top'>INC_NUMBER</td><td align='left' valign='top'>SERVICE</td><td align='left' valign='top'>ASSIGNED_GROUP</td><td align='left' valign='top'>ASSIGNEE</td><td align='left' valign='top'>STATUS</td><td align='left' valign='top'>DUEHOURS</td></tr><tr align='left' valign='top'><td align='left' valign='top'>INC</td><td align='left' valign='top'>GSI</td><td align='left' valign='top'>L2</td><td align='left' valign='top'>Mahen A</td><td align='left' valign='top'>Pending</td><td align='left' valign='top'>4</td></tr></table>

Many thanks in advance! 

那怎么能以表格格式而不是字符串显示???

4

1 回答 1

10

确保将 的IsBodyHtml属性设置MailMessagetrue

于 2013-04-09T14:24:28.307 回答