如何在电子邮件正文中插入HTML

时间:2017-10-04 10:39:47

标签: c#

在我的SharePoint网站中,如果将所有权重新分配给其他用户,我将生成电子邮件。这正常工作。但有时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;
    }

1 个答案:

答案 0 :(得分:0)

为你的css和htm提供单独的文件将使你的工作更轻松,代码将更易于维护。

然后您可以读取这两个文件并在html文件中插入值以进行发送。 在html文件中使用带有放置索引的花括号,您希望在其中插入一些值,如某些其他内容的标题。例如&lt; td&gt; {0}&lt; / td&gt;将在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
相关问题