2015-05-07 2 views
0

Когда я пытаюсь сгенерировать HTML-форматированный результат, заданный и отправляющий результирующий набор по электронной почте с использованием SMTP-настроек, он отлично работает в Outlook, но тот же html-формат не отображается в gmail. Он отображается как обычный текст.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 атрибуты

Удачи!

Смежные вопросы