c# - 在 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>
");
解决方案
<table>
为and添加样式时,您的语法错误<p>
。请按以下方式进行,例如:
<table style="width: 50%">
同样,在您使用样式的任何地方更改语法。
推荐阅读
- c++ - 未声明的标识符 - C++
- python - 无法在 Python 中使用 Selenium 访问弹出式登录表单
- android - 如果用户的密码直接在 Azure B2C 中更改,如何在我的 xamarin.forms 应用程序中知道。我正在使用 MSAL
- git - GPG 如何仅对特定的未签名提交进行签名?
- parameters - Prolog:如何编写计算积分的谓词?
- java - @Configuration 与@Service/@Controller/@Component 相比有什么好处
- xml - 通过 XSLT 的数字标签问题
- wordpress - 在 WordPress 中搜索具有具有多个值的元键的帖子的元数据
- javascript - 如何设置 localStorage 变量以匹配它所属的页面?
- google-sheets - 如何让谷歌表格(谷歌财经)显示今日收盘价