2015-05-07 73 views
0

當我試圖生成HTML格式化結果集並使用SMTPsettings作爲電子郵件發送結果集時,它在Outlook中工作正常,但在gmail中不顯示相同的htmlformat它顯示爲純文本。HTML格式的電子郵件不顯示在Gmail中

public void GenerateRpt() 
     { 
      DataSet ds= new DataSet(); 
      //Result set is assigned to the dataset object. 
      if ds[0].Rows.Count == 0) 
       return; 
      else 
      { 
       try 
       { 
        StringBuilder builder = new StringBuilder(); 
        builder.Append("<html xmlns='http://www.w3.org/1999/xhtml'>"); 
        builder.Append("<head>"); 
        builder.Append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"); 
        builder.Append(" <style type='text/css'> @page {} table { border-collapse: collapse;border-spacing: 0;empty-cells: show} "); 
        builder.Append(" .Default { font-family: Calibri;background-color: transparent;border-style: none;vertical-align: bottom;margin-left: 0in;"); 
        builder.Append(" writing-mode: page;color: #000000;font-size: 11pt; font-style: normal;text-shadow: none; text-decoration: none ! important;font-weight: normal;}"); 
        builder.Append(" .ce2 { background-color: #95b3d7;border-width: 0.0349cm;border-style: solid;border-color: #000000;color: #000000;font-size: 11pt;"); 
        builder.Append("font-style: normal;font-weight: bold;margin-left: 0in;text-shadow: none;font-family: Calibri;text-decoration: none ! important;"); 
        builder.Append("vertical-align: middle;writing-mode: page;text-align: center ! important;}"); 
        builder.Append(" .ce5 { background-color: transparent;border-width: 0.0349cm;border-style: solid;border-color: #000000;color: #000000;font-size: 11pt;"); 
        builder.Append("font-style: normal;font-weight: normal;margin-left: 0in;text-shadow: none;font-family: Calibri;text-decoration: none ! important;"); 
        builder.Append("vertical-align: middle;writing-mode: page;text-align: center ! important;}"); 
        builder.Append(" .ce6 { background-color: #a6a6a6;border-width: 0.0349cm;border-style: solid;border-color: #000000;color: #000000;font-size: 11pt;"); 
        builder.Append("font-style: normal;font-weight: bold;margin-left: 0in;text-shadow: none;font-family: Calibri;text-decoration: none ! important;"); 
        builder.Append("vertical-align: middle;writing-mode: page;text-align: center ! important;}"); 
        builder.Append(" .ce13 { background-color: transparent;border-width: 0.0349cm;border-style: solid;border-color: #000000;color: #000000;font-size: 11pt;"); 
        builder.Append("font-style: normal;font-weight: normal;margin-left: 0in;text-shadow: none;font-family: Calibri;text-decoration: none ! important;"); 
        builder.Append("vertical-align: bottom;writing-mode: page;}"); 
        builder.Append(" .contentText {font-size: 11pt;font-weight: normal;font-style: normal;font-style: Calibri;font-weight: bold;COLOR: #cccccc;"); 
        builder.Append("</style>"); 
        builder.Append("</head>"); 
        builder.Append("<body>"); 
        builder.Append("<table border='0' cellpadding='1' cellspacing='1' width='70%'> "); 
        builder.Append("<tr class='ro1'><td colspan='4'><b><p>RPT NAME</p></b></td></tr>"); 
        builder.Append("<tr class='ro1'>"); 
        builder.Append("<td class='ce2'><p>COL1</p></td>"); 
        builder.Append("<td class='ce2'><p>COL2</p></td>"); 
        builder.Append("<td class='ce2'><p>COL3</p></td>"); 
        builder.Append("<td class='ce2'><p>COL4</p></td>"); 
        builder.Append("</tr>"); 
        string tempdrow = string.Empty; 
        int i; 
        for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) 
        { 
         builder.Append("<td class='ce5'>"); 
         builder.Append("<p>" + ds.Tables[0].Rows[i]["Col1"].ToString() + "</p>"); 
         builder.Append("</td>"); 
         builder.Append("<td class='ce13' style='text-align: center'>"); 
         builder.Append("<p>" + ds.Tables[0].Rows[i]["Col2"].ToString() + "</p>"); 
         builder.Append("</td>"); 
         builder.Append("<td class='ce13' style='text-align: center'>"); 
         builder.Append("<p>" + ds.Tables[0].Rows[i]["Col3"].ToString() + "</p>"); 
         builder.Append("</td>"); 
         builder.Append("<td class='ce13' style='text-align: center'>"); 
         builder.Append("<p>" + ds.Tables[0].Rows[i]["Col4"].ToString() + "</p>"); 
         builder.Append("</td>"); 
         builder.Append("</tr>"); 
        } 
        // When No more records in the Dataset show the Grand Total. 
        builder.Append("<tr class='ro2'>"); 
        builder.Append("<td class='ce13' style='text-align: center'>"); 
        builder.Append("<b><p>GRAND TOTAL </p></b>"); 
        builder.Append("</td>"); 
        builder.Append("<td class='ce13' style='text-align: center'>"); 
        builder.Append("<p>" + ds.Tables[0].Rows[i - 1]["Col2"].ToString() + "</p>"); 
        builder.Append("</td>"); 
        builder.Append("<td class='ce13' style='text-align: center'>"); 
        builder.Append("<p>" + ds.Tables[0].Rows[i - 1]["Col3"].ToString() + "</p>"); 
        builder.Append("</td>"); 
        builder.Append("<td class='ce13' style='text-align: center'>"); 
        builder.Append("<p>" + ds.Tables[0].Rows[i - 1]["Col4"].ToString() + "</p>"); 
        builder.Append("</td>"); 
        builder.Append("</tr>"); 
        builder.Append("</table>"); 
        string sVehRejsubject = System.Configuration.ConfigurationManager.AppSettings["Subject"].ToString(); 
        sendmail(builder.ToString(), sVehRejsubject); 
       } 
       catch (Exception ex) 
       { 
        ex.Message(); 

       } 

      } 

     } 

// Mail Sending Part 


void sendmail(string strResultSet,string strRptType) 
     { 
      try 
      { 
       if (strResultSet.ToString() != "") 
       { 
        string strMessage1 = string.Empty, TestEmp = "Mani"; 
        strMessage1 = strResultSet.ToString(); 
        strMessage1 += "<br><br><font color='blue'><b>Date:</b></font> " + DateTime.Now.ToString("dd/MM/yyyy") + "<BR>"; 
        strMessage1 += "<br>Regards,<BR><BR>"; 
        strMessage1 += TestEmp + "</b><br>"; 
        <BR></td></tr></table></body></html>"; 

        MailMessage mail = new MailMessage(); 
        //SMTP SETTINGS DEFINED IN APP.CONFIG FILE 
        System.Net.Mail.SmtpClient _client = new System.Net.Mail.SmtpClient(); 
        string sFrom = System.Configuration.ConfigurationManager.AppSettings["emailFrom"].ToString(); 
        string sTo = System.Configuration.ConfigurationManager.AppSettings["emailTo"].ToString(); 
        string sCC = System.Configuration.ConfigurationManager.AppSettings["emailCC"].ToString(); 
        string sBCC = System.Configuration.ConfigurationManager.AppSettings["emailBCC"].ToString(); 
        string sDisplayName = System.Configuration.ConfigurationManager.AppSettings["emailFromDisplayName"].ToString(); 
        mail.From = new MailAddress(sFrom, sDisplayName); 
        mail.To.Add(new MailAddress(sTo)); 
        if (sCC.ToString() != "") 
        { 
         mail.CC.Add(sCC); 
        } 
        if (sBCC.ToString() != "") 
        { 
         mail.Bcc.Add(sBCC); 
        } 
        mail.IsBodyHtml = true; 
        mail.Subject = strRptType + " AS ON - " + System.DateTime.Now.ToString("dd/MM/yyyy"); 
        mail.Body = strMessage1; 
        _client.Send(mail); 
       } 
      } 
      catch (Exception ex) 
      { 
       Ex.Message(); 
       return; 

      } 

     } 

回答

2

電子郵件提供商,不允許將html標籤的整個頻譜包含在消息中。如果您嘗試添加不受支持的標籤,它將被忽略或自動更改爲其他標籤。

這與不支持的css屬性相同。

例如<br/>是不允許的,將被忽略

你應該做的是隻使用普遍支持的HTML標籤。

我發現this頁面列出了普遍支持的HTML標籤和CSS屬性

祝你好運!

相關問題