首页 > 解决方案 > 在 c# 的电子邮件正文中为表格添加样式

问题描述

我想用一些基本样式的表格撰写邮件。这是我的代码:

                MailMessage mail = new MailMessage();           
                mail.From = new MailAddress("abc@domain.com");
                mail.To.Add("myname@domain.com");
                mail.Body = string.Format(@"<html>
                                            <body>
                                            <style>
                                            table, th, td {
                                              border: 1px solid black;
                                              line-height: 24px;
                                              font-size:14px;
                                              font-family:verdana;
                                              text-align: center;
                                            }
                                            </style>
                                            <p style=""""font-family:verdana""""> Status :</p>
                                            
                                            <table style=""""width=50%""""><tr>
                                                             <th >No. of Students</th> 
                                                             <th >No. of Pass</th> 
                                                             <th >No. of Failures </th>                 
                                                          <tr/><tr style=""""height=120%""""> 
                                                            <td>{0} </td> 
                                                            <td style=""""color:green"""">{1} </td>
                                                            <td style=""""color:red"""">{2} </td> 
                                                          </tr></table>
                                            <p style=""""font-family:verdana""""> Please refer to the 
                                          Log files for more details.</p>
                                            </body>
                                            </html>",student.Total, student.Pass,student.Fail);
                mail.Subject = "Report";
            };

            mail.IsBodyHtml = true;

这给了我一个异常,说“输入字符串的格式不正确”。如果我删除样式、段落并仅保留,它可以工作,但表格中没有边框。如何设置邮件样式?

我是这样换了身体的,但风格没有体现出来。

mail.Body = string.Format(@"
                                           
                                            <p style=""font-family:verdana""> Status :</p>

                                            <table style=""border: 1px solid black"", ""text-align: center""><tr>
                                                             <th style=""border: 1px solid black"">No. of new records added</th> 
                                                             <th style=""border: 1px solid black"">No. of records transferred </th> 
                                                             <th style=""border: 1px solid black"">No. of records not transferred </th>                 
                                                          <tr/><tr style=""height=120%""> 
                                                            <td style=""border: 1px solid black"" ""text-align: center"">CompanyName.Value  </td> 
                                                            <td style=""border: 1px solid black"" ""text-align: center"" ""color:green"">1000 </td>
                                                            <td style=""border: 1px solid black"" ""text-align: center"" ""color:red"">82  </td> 
                                                          </tr></table>
                                            <p style=""font-family:verdana""> Please refer to the Log files for more details.</p>
                                            ");

标签: c#htmlemail

解决方案


<table>为and添加样式时,您的语法错误<p>。请按以下方式进行,例如:

<table style="width: 50%">

同样,在您使用样式的任何地方更改语法。


推荐阅读