简体   繁体   中英

C# System.Net.Mail CSS Error in Outlook 2013

I am using the following in my C# app to add a little bit of formatting to the email's that are being sent. It display just fine in my mobile client email android and ios as well as outlook 2011 for mac but I am not getting any formatting in Outlook 2013 it self. I have looked at www.campaignmonitor.com/css and thought I had everything working correctlt

public static void Send_Email()
    {
        StringBuilder sb = new StringBuilder();
        StreamReader sr = new StreamReader("Email.css");
        string[] files = Directory.GetFiles(sDirectory + "-" + sSearchTerm);

        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("<out bound server>");

        mail.From = new MailAddress("<from address>");
        mail.To.Add(sEmailAddress);
        mail.Subject = "Logs from " + Environment.GetEnvironmentVariable("COMPUTERNAME") + " searched " + sDate + " for " + sSearchTerm;

        sb.AppendLine("<table>");

        sb.AppendLine("</table>");

        sb.AppendLine("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>");
        sb.AppendLine("<html>");
        sb.AppendLine("<head>");
        sb.AppendLine("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
        sb.AppendLine("<style type='text/css'>");
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            sb.AppendLine(line);
        }
        sb.AppendLine("</style>");
        sb.AppendLine("</head>");
        sb.AppendLine("<body>");
        sb.AppendLine("Here are the list of files on <b>" + Environment.GetEnvironmentVariable("COMPUTERNAME") + "</b> for date " + sDate + " that contain(s) <b><i>" + sSearchTerm + "</i></b><br/><br/>");
        sb.AppendLine("<table>");
        sb.AppendLine("  <tr>");
        sb.AppendLine("    <th>Filename</th>");
        sb.AppendLine("  </tr>");

        foreach (var file in files)
        {
            sb.AppendLine("<tr><td>" + Path.GetFileName(file) + "</td></tr>");
        }

        sb.AppendLine("</table>");
        sb.AppendLine("</body>");
        sb.AppendLine("</html>");

        mail.IsBodyHtml = true;

        mail.Body = sb.ToString();
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new NetworkCredential("<user>", "<pass>");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);
    }

Here is the css I am parsing in

 table, thead, th, tr, td {
   border: 1px solid black;
   padding:5px;
   border-collapse:collapse;
  }

 table {
   margin-bottom:10px;
 }

 th {
   background:grey;
   color:white;
 }

 tbody tr:nth-child(even) {
   background-color: #bada55;
 }

 tbody tr:nth-child(odd) {
   background-color: lightblue;
 }

Here is the html code behind the message as displayed

 <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'       'http://www.w3.org/TR/html4/loose.dtd'><html><head>
 <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
 <style type="text/css">
 table, thead, th, tr, td {
  border: 1px solid black;
  padding:5px;
  border-collapse:collapse;
 }

 table {
  margin-bottom:10px;
 }

 th {
  background:grey;
  color:white;
 }

 tbody tr:nth-child(even) {
   background-color: #bada55;
 }

 tbody tr:nth-child(odd) {
   background-color: lightblue;
 }
 </style>
 </head>
 <body>
 Here are the list of files on <b>CO200197L</b> for date 2014-09-02 that contain(s) <b><i>c</i>     </b><br><br>
<table>
<tr>
  <th>Filename</th>
</tr>
<tr><td>inin.bcf.smartclient.ininlog</td></tr>
<tr><td>inin.updateclientapp.ininlog</td></tr>
<tr><td>interactionclient.ininlog</td></tr>
<tr><td>screencaptureclientu.ininlog</td></tr>
<tr><td>screencaptureclientu_1.ininlog</td></tr>
</table>
</body>
</html>

Here are the results first two images are outlook 2013 / owa, other are mobile email and outlook for mac

外表OWAiOS-它应该看起来如何Outlook 2011-Mac

Any ideas?

使用内联样式(即style属性)代替style元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM