HTML电子邮件无法正确显示

时间:2010-09-10 02:00:44

标签: c# html css markup mailmessage

我在c#中发送HTML邮件时遇到一个奇怪的问题。

基本上我每天早上都会给自己发送电子邮件,我首先从ftp网站下载HTML标记中的天气。

获取源文件后,我将其读入字符串并使用以下代码创建mailMessage。

string body = File.ReadAllText(@"C:\Weather.htm");

MailMessage mailMessage = new MailMessage();
SmtpClient mailClient = new SmtpClient("smtp.gmail.com");

mailClient.Credentials = new NetworkCredential(username, password);
mailClient.Port = 587;
mailClient.EnableSsl = true;
mailMessage.From = new MailAddress(emailFrom);
mailMessage.IsBodyHtml = true;    
mailMessage.To.Add(emailTo);
mailMessage.Subject = "Test Email";
mailMessage.SubjectEncoding = System.Text.Encoding.Unicode;
mailMessage.Body = body;
mailMessage.BodyEncoding = System.Text.Encoding.Unicode;
mailClient.Send(mailMessage);

问题在于,当电子邮件以HTML格式到达时,所有DIV / CSS都不受尊重,看起来很奇怪。我试图通过电子邮件发送到其他电子邮件地址&客户端看起来和尝试不同的编码一样。

因此,某些地方出现了问题。

有谁知道如何解决这个问题并获得格式正确的电子邮件?

良好形象

alt text

BadImage

alt text

源标记

<html xmlns="http://www.w3.org/1999/xhtml"><head xmlns="">
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Sydney Forecast</title>
        <link rel="stylesheet" type="text/css" href="http://www.bom.gov.au/watl/standard/common.css">
        <link rel="stylesheet" type="text/css" href="http://www.bom.gov.au/weather-services/styles/text-gfe.css">
    </head>
    <body>
        <div class="product">
            <p xmlns="" class="p-id">IDN10064</p>
            <p xmlns="" class="source">Australian Government Bureau of Meteorology<br/>New South Wales
            <h2 xmlns="">Updated Sydney Forecast</h2>
            <p xmlns="" class="date">Issued at 8:11 am&nbsp;EST on Thursday 9 September 2010<br>for the period until midnight EST&nbsp;Wednesday 15 September 2010.</p>
            <h3 xmlns="" class="warning">Warning Summary at issue time</h3>
            <p xmlns="">Nil.</p>
            <p xmlns="" class="p-id">Details of warnings are available on the Bureau's website www.bom.gov.au, by telephone 1300-659-218* or through some TV and radio broadcasts.</p>
            <h3 xmlns="" class="day">Forecast for the rest of Thursday</h3>
            <p xmlns="" class="sl">Cloud increasing. Areas of rain this afternoon and evening. Winds northerly averaging up to 20 km/h.</p>
            <div xmlns="" class="grid">
                <div class="line space-b">
                    <div class="name">City Centre</div>
                    <div class="wx">A little rain later.</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">20</div>
                </div>
                <div class="line">
                    <div class="rain_l">Chance of any rainfall:</div>
                    <div class="rain_prob_v">30%</div>
                    <div class="rain_l">Chance of no rainfall:</div>
                    <div class="rain_prob_v">70%</div>
                    <div class="rain_l">Rainfall:</div>
                    <div class="rain_v">0 to 1 mm</div>
                </div>
                <div class="line space-b">
                    <div class="name">Penrith</div>
                    <div class="wx">Late rain.</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">20</div>
                </div>
                <div class="line">
                    <div class="rain_l">Chance of any rainfall:</div>
                    <div class="rain_prob_v">30%</div>
                    <div class="rain_l">Chance of no rainfall:</div>
                    <div class="rain_prob_v">70%</div>
                    <div class="rain_l">Rainfall:</div>
                    <div class="rain_v">0 to 1 mm</div>
                </div>
                <div class="line">
                    <div>
                        <h4>Around Sydney</h4>
                    </div>
                </div>
                <div class="line">
                    <div class="name">Liverpool</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">20</div>
                    <div class="name">Parramatta</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">20</div>
                </div>
                <div class="line">
                    <div class="name">Terrey Hills</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">19</div>
                    <div class="name">Campbelltown</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">19</div>
                </div>
                <div class="line">
                    <div class="name">Richmond</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">20</div>
                    <div class="name">Bondi</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">19</div>
                </div>
            </div>
            <p xmlns="" class="sl">Fire Danger: Low to Moderate [0-11]</p>
            <p xmlns="" class="sl">UV Alert from 9:10 am to 2:40 pm, UV Index predicted to reach 6 [High]</p>
            <h3 xmlns="" class="day">Forecast for Friday</h3>
            <p xmlns="" class="sl">Partly cloudy. Winds west to northwesterly averaging up to 25 km/h tending westerly up to 40 km/h around midday.</p>
            <div xmlns="" class="grid">
                <div class="line space-b">
                    <div class="name">City Centre</div>
                    <div class="wx">Becoming windy.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">13</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">22</div>
                </div>
                <div class="line space-b">
                    <div class="name">Penrith</div>
                    <div class="wx">Partly cloudy. Becoming windy.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">11</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">23</div>
                </div>
            </div>
            <h3 xmlns="" class="day">Forecast for Saturday</h3>
            <p xmlns="" class="sl">Sunny. Winds west to southwesterly averaging up to 25 km/h tending mainly southeast to southwesterly up to 20 km/h around midday.</p>
            <div xmlns="" class="grid">
                <div class="line space-b">
                    <div class="name">City Centre</div>
                    <div class="wx">Sunny.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">12</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">21</div>
                </div>
                <div class="line space-b">
                    <div class="name">Penrith</div>
                    <div class="wx">Sunny.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">9</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">22</div>
                </div>
            </div>
            <h3 xmlns="" class="day">Forecast for Sunday</h3>
            <p xmlns="" class="sl">Mostly sunny. Light winds.</p>
            <div xmlns="" class="grid">
                <div class="line space-b">
                    <div class="name">City Centre</div>
                    <div class="wx">Mostly sunny.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">10</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">21</div>
                </div>
                <div class="line space-b">
                    <div class="name">Penrith</div>
                    <div class="wx">Sunny.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">6</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">23</div>
                </div>
            </div>
            <h3 xmlns="" class="day">Forecast for Monday</h3>
            <p xmlns="" class="sl">Becoming cloudy. Isolated showers later in the day. Winds west to southwesterly averaging up to 25 km/h.</p>
            <div xmlns="" class="grid">
                <div class="line space-b">
                    <div class="name">City Centre</div>
                    <div class="wx">Shower or two developing.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">13</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">22</div>
                </div>
                <div class="line space-b">
                    <div class="name">Penrith</div>
                    <div class="wx">Shower or two developing.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">10</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">22</div>
                </div>
            </div>
            <h3 xmlns="" class="day">Forecast for Tuesday</h3>
            <p xmlns="" class="sl">Sunny. Light winds tending north to northeasterly up to 20 km/h during the evening.</p>
            <div xmlns="" class="grid">
                <div class="line space-b">
                    <div class="name">City Centre</div>
                    <div class="wx">Sunny.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">12</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">19</div>
                </div>
                <div class="line space-b">
                    <div class="name">Penrith</div>
                    <div class="wx">Sunny.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">10</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">19</div>
                </div>
            </div>
            <h3 xmlns="" class="day">Forecast for Wednesday</h3>
            <p xmlns="" class="sl">Isolated showers during the morning. Sunny afternoon. Winds northwesterly averaging up to 25 km/h tending westerly up to 35 km/h during the morning.</p>
            <div xmlns="" class="grid">
                <div class="line space-b">
                    <div class="name">City Centre</div>
                    <div class="wx">Shower or two clearing.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">12</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">20</div>
                </div>
                <div class="line space-b">
                    <div class="name">Penrith</div>
                    <div class="wx">Shower or two clearing.</div>
                    <div class="min_l">Min</div>
                    <div class="min_v">8</div>
                    <div class="max_l">Max</div>
                    <div class="max_v">21</div>
                </div>
            </div>
            <p xmlns="" class="dt">The next routine forecast will be issued at 4:20 pm&nbsp;EST&nbsp;Thursday.</p>
            <p xmlns="" class="p-id">* Calls to 1300 numbers cost around 27.5c incl. GST, higher from mobiles or public phones.</p>
        </div>
</html>

4 个答案:

答案 0 :(得分:6)

电子邮件不支持网页支持的css设置。所以内容的变化是正常的。

另外,建议不要在电子邮件中使用div,因为对于电子邮件内容,表格是更安全的选项。

检查链接: http://css-tricks.com/using-css-in-html-emails-the-real-story/ http://www.campaignmonitor.com/css/

答案 1 :(得分:5)

如果没有应用CSS,“坏”图像看起来与HTML呈现的方式完全一样。如果您使用的是网络邮件,请使用类似HTTPFox的内容来检查浏览器是否请求CSS文件 - 它们可能会作为反垃圾邮件措施的一部分得到抑制(不会自动加载远程内容)。如果它是一个真正的邮件客户端(Outlook,Thunderbird等...)使用线控嗅探器并进行相同的检查。

您很可能必须下载css并将其嵌入到内联中,最好是在<body>标记之后的某个位置。

答案 2 :(得分:2)

许多电子邮件客户端会删除身体标记上方显示的所有内容(即您的CSS链接) - 如果您执行所有CSS内联会发生什么?

This link 可能会提供一些有用的帮助

答案 3 :(得分:1)

我最近不得不做这种类型的项目。尝试写入最低公分母,例如OE 6和之前。 OE 6.0和之前版本在CSS的表示方面不一致 - 即使它嵌入在标题中。与身体中的DIV相同。

(取自Yahoo Answers 09-09-2010 1

  

最佳答案 - 选民选择   这肯定取决于哪个   您正在使用的Outlook版本。任何   这样做,可能只支持内联   样式...

 <p style=" color:red; ">this is red</p>
  

...但肯定没有联系风格   床单,可能不是样式表   在头脑中。

建议的解决方案:将源行转换为HTML 2.0格式的表格行。

从源代码中,使用嵌入式样式以编程方式将每一行转换为HTML 2.0表格行。然后在主体中追加每个表行。最后,关闭桌子。

这是获得所需结果的最可靠方法(远程加载预格式化图像)。

如果要在表格中包含图像,可以远程加载它们,或者,可以将它们作为附件插入,并引用表格主体中的附件。我尝试使用64位图像编码,但在电子邮件客户端中并不一致。我选择了附件方法,因为我不想依赖远程加载图像的服务器。