我一直在努力做某事。我有一个控制台应用程序,用于查找某些文件(如果不存在)。因此,此应用程序将电子邮件发送到我的电子邮件ID。将为电子邮件格式创建普通的SuccessBody.html。
现在,我正在尝试在message.html中动态添加html表,如下所示。
HTML
<body>
Hi All,
<br />
<br />
Please be informed that below files for this month <b>are available</b>.
<br />
<br />
<table align="center" ; width="1000" style="border-collapse:collapse; border-color:#17202A; text-align:center;">
<tr height="30" ; style="background-color:#CED8F6; color:#17202A; font-weight:bold; font-size:20px; font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;">
<td colspan="2" ; style=" border-color:#FFFFFF; border-style:solid; border-width:thick; padding: 5px;">$Title$</td>
</tr>
<tr style="background-color:#143665; color:#FFFFFF; font-weight:bold; font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;">$Columns$</tr>
$Rows$
</table>
<br />
<br />
<b>Please take necessary actions.</b>
<br />
<br />
Regards,
<br />
Ricky
</body>
我不想在代码本身中设计此表,而是通过Config文件。关键是循环设计。$ Title $,$ Column $和$ Rows $是我想用以下C#中的某些代码替换的字符串:
C#
public static void DataTableToHTML(DataTable Entries)
{
string mailbody = string.Empty;
if (Entries.Rows.Count.Equals(0))
{
string email_body_error = File.ReadAllText(ConfigurationManager.AppSettings["Body_Fail"]);
mailbody = email_body_error;
}
else
{
string email_body_success = string.Empty;
string html = string.Empty;
TextWriter writehtmlFile = new StreamWriter(Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName) + ".html");
string ConfigTitle = ConfigurationManager.AppSettings["Title"];
File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Title$", ConfigTitle);
for (int i = 0; i < Entries.Columns.Count; i++)
{
html += ConfigurationManager.AppSettings["ForColumns"] + Entries.Columns[i].ColumnName + "</td>";
}
File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Columns$", html);
//add rows
for (int i = 0; i < Entries.Rows.Count; i++)
{
html += ConfigurationManager.AppSettings["ForRows"];
for (int j = 0; j < Entries.Columns.Count; j++)
html += ConfigurationManager.AppSettings["RowData"] + Entries.Rows[i][j].ToString() + "</td>";
html += "</tr>";
}
writehtmlFile.WriteLine(html);
writehtmlFile.Flush();
writehtmlFile.Close();
email_body_success = File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Rows$", html);
mailbody = email_body_success;
}
SendEmail(mailbody, DateTime.Now);
}
我想在配置文件(AppConfig.xml)中配置此表的设置,然后从上面的代码中使用它。
配置文件
<add key="FileName" value="File Name"/>
<add key="Date" value="Creation Date"/>
<add key ="ForColumns" value="<td style=\" border-color:#FFFFFF; border-style:solid; border-width:thick; padding: 5px;\">"/>
<add key="ForRows" value="<tr style =\"color:#17202A; font-weight:bold; font-family: Century Gothic,CenturyGothic,AppleGothic,sans-serif;\" >"/>
<add key="RowData" value="<td style=\" border-color:#FFFFFF; background-color:#F3E2A9; border-style:solid; border-width:thick; padding: 5px;\">"/>
<add key="Body_Success" value="HTMLBody\SuccessBody.html"/>
Body_Success是SuccessBody.html的位置=应用程序的位置。
$ Title $很容易被替换。但是,$ Column $和$ Rows $并没有像我希望的那样被替换。如果有人可以帮助我,那真是太好了。
在.Net(和许多其他工具栈)中,字符串是不可变的。这意味着一旦完成:
string foo = "bar";
如果您检查变量foo
,您将始终找到“ bar”。如果您希望foo
成为其他名称,则必须为其分配一个新值:
string foo = "fubar";
string huh = foo.Replace("fu", "foo");
同样的原理也适用于您在字符串上调用的方法,例如Replace
。该方法返回一个新字符串,在该字符串中原始字符串被新值替换。因此,在上面的示例中,huh
的值为“ foobar”,其中foo
仍为“ fubar”。
为此在线:
File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Title$", ConfigTitle);
您要将替换的结果放在地板上。
相反,这样做:
// read the template from the file once and put it in a variable
email_body_success = File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]);
// replace the title by storing the replaced value again the same variable
email_body_success = email_body_success.Replace("$Title$", ConfigTitle);
[从现在开始,email_body_success
将使用带有(部分)替换的模板的字符串。您不需要(这样做是错误的)再次调用File.ReadAlltext
。因为如果这样做,您将再次从文件中读取模板,丢弃已被替换的内容。
现在我们进入两个循环,分别是列和行。建立所需的内容后,html
的内容需要用email_body_success
中的标记替换。
而不是:
File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Columns$", html);
做
// replace the columns
email_body_success = email_body_success.Replace("$Columns$", html);
html = String.Empty; // html is stored, let's reset for its next use.
因为现在email_body_success
将用$ Columns $的新html替换我们以前的部分模板。在Rows循环的开始,您想将html
字符串再次重置为空,这样它就仍然不会容纳列]
在“行”循环的末尾,需要应用相同的修复程序。替换
email_body_success = File.ReadAllText(ConfigurationManager.AppSettings["Body_Success"]).Replace("$Rows$", html);
与
// replace the rows
email_body_success = email_body_success.Replace("$Rows$", html);
这应该在email_body_success
中为您提供带有替换部分的模板。
我不确定您要通过此行实现什么:
writehtmlFile.WriteLine(html);
但是如果您打算将整个模板写入该文件,则需要用html
变量替换email_body_success
,并在最后一次替换之后将这些行移动。
I would suggest -
Create html file and save email html part along with keywords which you want to replace like as ForColumns ,ForRows.
e.g.
<html>
<body>
<table>
<thead>
<th>ForColumns1 </th>
<th>ForColumns2 </th>
</thead>
<tr>ForRows1</tr>
<tr>ForRows2</tr>
</table>
</body>
</html>
while sending mail get replace keyword(ForColumns,ForRows) value from config and replace with html file data.