HTML格式化的电子邮件未在Gmail中显示

时间:2015-05-07 06:17:18

标签: c# email

当我尝试生成HTML格式化结果集并使用SMTPsettings将结果集作为电子邮件发送时,它在Outlook中正常工作但gmail中没有显示相同的html格式它显示为纯文本。

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;

            }

        }

1 个答案:

答案 0 :(得分:2)

电子邮件提供商不允许将整个html标记包含在邮件中。如果您尝试包含不受支持的标记,它将被忽略或自动更改为另一个标记。

不受支持的css属性也是如此。

例如,<br/>不被允许,将被忽略

您应该做的只是使用普遍支持的Html标记。

我找到了this页面,其中列出了普遍支持的html标记和css属性

祝你好运!