2017-10-04 138 views
0

在我的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; 
    } 
+0

集電子郵件對象IsBodyHtml屬性爲true – Damirchi

+0

我想我們主要是需要看你怎麼用了GenerateTD現在。簡而言之,它似乎只是錯過了<'s – BugFinder

+0

檢查你是否在所有地方都正確使用了escape \,然後檢查你是否混淆了'和'某處。如果你錯過了一個本應該是HTML標籤的東西,解析爲文本 – DanteTheSmith

回答

0

具有你的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 
相關問題