5

我对 StringBuilder 类的 AppendFormat 方法有疑问。我正在创建表格并在字符串构建器对象中附加数据以将其作为邮件发送,但是当我看到我发送的邮件时,它看起来不像表格,它的标题和相应的内容被错放。我希望表格由 Lines 分隔,因为它通常存在于 microsoft word 的表格中。我怎样才能实现它..我正在使用以下代码: Body is a StringBuilder Object

if (  dic1 != null )
{
     //Body.AppendFormat("Client: " + cl + " Success. " + dic1.Count + " :");
     Body.AppendFormat("<br/><table>");
     Body.AppendFormat("<h1><tr><td>#</td><td>Files Name</td></tr></h1>");
     int count = 1;
      foreach ((KeyValuePair<string, string> pair in  dic1)
               {
                    if (!String.IsNullOrEmpty(pair.Key))
                     {

                      Body.AppendFormat("<tr><td>"+count.ToString()+"</td><td>" + pair.Key + "</td><td> " + pair.Value + "</td></tr>");
                        count++;
                        //Body.Append( );
                    }
                }

                Body.AppendFormat("</table>");

以下是我在收件箱中收到的输出。

 # File Name Error 
 1 txt1.txt Loading File 'txt1.txt' failed: The specified File already exists in the system 
 2 txt2.txt Loading File 'txt2.txt' failed: The specified File already exists in the system 
 3 txt3.txt Loading File 'txt3.txt' failed: The specified File already exists in the system 
4

3 回答 3

4

希望你可以使用这个例子来理解它应该是怎样的。如果没有,请告诉我。会帮助你。

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<table>");
sb.AppendFormat("<tr><td>Request Name:</td><td>{0}</td></tr>", txtBugName.Text.Trim());
sb.AppendFormat("<tr><td>Category:</td><td>{0}</td></tr>", ddlModule.SelectedValue);
sb.AppendFormat("<tr><td>Sub-Category:</td><td>{0}</td></tr>", ddlPage.SelectedValue);
sb.AppendFormat("<tr><td>Description:</td><td>{0}</td></tr>", txtComments.Text.Trim());
sb.AppendFormat("<tr><td>Email is:</td><td>{0}</td></tr>", txtemail.Text.Trim());
sb.Append("<table>");

然后我假设 IsBodyHtml 属性为真,因为您已经在使用 HTML

于 2012-09-10T11:03:31.900 回答
0

做这样的事情

    String to, subject, message;
    bool isHtml;
    isHtml = true;
    StringBuilder sbEmail = new StringBuilder();

    to = "abc@abc.com";
   // string url = "xxxxx";

    //  string refurl = Request.UrlReferrer.ToString();
    subject = "Some subject";

    // Keep Option for Otehr Language also by defining predefined 
    // Hidden Labels for Message in L1, L2, L3, L4, L5 --
    // Also add dir tag to the Table
    sbEmail.Append("<table><tr><td style='font-family: Arial; font-size: 10pt;'>");
    sbEmail.Append("Hello,");
    sbEmail.Append("<br><br>");
    sbEmail.Append("");
    sbEmail.Append("Following Comments has been posted");
    sbEmail.Append("<br><br>");
    sbEmail.Append(strComment);
    sbEmail.Append("<br><br>");
    sbEmail.Append("Article Title :");
    sbEmail.Append("<br>");
    sbEmail.Append(strArticleTitle);
    sbEmail.Append("<br><br>");
    sbEmail.Append("Name : " + strName);
    sbEmail.Append("<br>");
    sbEmail.Append("Country : " + strCountry);
    sbEmail.Append("<br><br>");
    sbEmail.Append("Please logon to admin section of website to activate this comments");
    sbEmail.Append("<br>");
    sbEmail.Append("http://xxxxx");
    sbEmail.Append("</td></tr></table>");
    message = sbEmail.ToString();
于 2012-09-10T11:03:15.197 回答
0

要获得所需的边框,您应该在表格和标签上合并一个内联样式。 <table cellpadding='5' cellspacing='2' style='border:1px solid black;'> <td style='border: 1px solid black;'> 这应该给出预期的风格。

于 2020-08-28T17:42:27.223 回答