如何在电子邮件正文中插入HTML
问题描述:
在我的SharePoint网站中,如果将所有权重新分配给其他用户,我将生成电子邮件。此工作正常。但某些时候,html标记在电子邮件中显示。如何在电子邮件正文中插入HTML
如何避免这种情况,它不经常发生。有没有其他的方式来写电子邮件?
这里是我的代码:
public StringBuilder GTd(string strTitle, string strValue)
{
StringBuilder sbBody = new StringBuilder();
sbBody.Length = 0;
sbBody.Capacity = 0;
try
{
sbBody.Append("<tr>");
sbBody.Append("<td height=\"25\" style=\"padding-right:20px;font-family:Arial,Sans-serif;font-size:13px;color:#888;white-space:nowrap; font-style:normal;\">" + strTitle + "</td>");
sbBody.Append("<td height=\"25\" style=\"padding-right:10px;font-family:Arial,Sans-serif;font-size:13px;color:#888;\">•</td>");
// sbBody.Append("<td height=\"25\" style=\"padding-right:10px; font-size:13px; color:#222\"; margin:0=\"margin:0\" 0 0.3em 0;\">" + strValue + "</td>");
sbBody.Append("<td height=\"25\" style=\"padding-right:10px; font-size:13px; color:#222; margin:0=\"margin:0\" 0 0.3em 0;\">" + strValue + "</td>");
sbBody.Append("</tr>");
}
catch (Exception ex)
{
}
return sbBody;
}
答
具有你的CSS和HTM单独的文件会使事情变得更容易为你的代码会更容易维护。
然后您可以读取这两个文件并在HTML文件中插入值进行发送。 在您的html文件中使用带放置索引的大括号,您希望在其中插入一些值,如其他一些内容的标题。例如< TD> {0} </TD>将地方插入一个标题在HTML文件中
var cssPath = "path to css";
var html ="";
var htmlFile = "path to html file"
using(StreamReader rd = new StreamReader(htmlFile))
{ html = rd.ReadEnd();
html = string.Format(html,value1,value2);//remember the values will be placed inside
the curly braces in the html file. and the number
of curly braces and arguments should match in exact order.
}
//Now that you have your html file with the
contents you want inside, you need to insert
the css file into the html.
using(StreamReader rd = new StreamReader(cssPath))
{
var css = rd.ReadToEnd();
var index= html.IndexOf("<head>") +6;
html = html.Insert(index, css)
}
// then remember to set IsBodyHtml = true in your mail message object
集电子邮件对象IsBodyHtml属性为true – Damirchi
我想我们主要是需要看你怎么用了GenerateTD现在。简而言之,它似乎只是错过了 BugFinder
检查你是否在所有地方都正确使用了escape \,然后检查你是否混淆了'和'某处。如果你错过了一个本应该是HTML标签的东西,解析为文本 – DanteTheSmith