2012-10-02 99 views
0

我試圖從我的程序發送郵件與MailDefinition類。當我嵌入一些簡單的html時它工作正常。我的代碼如下:發送電子郵件與C#中的HTML顯示額外的空格

MailDefinition md = new MailDefinition(); 
     md.From = "me"; 
     md.IsBodyHtml = true; 
     md.Subject = "Test of MailDefinition"; 

     string body = System.IO.File.ReadAllText(@"C:\my.html"); 

     MailMessage msg = md.CreateMailMessage("[email protected]", replacements, body, new System.Web.UI.Control()); 
     NetworkCredential loginInfo = new NetworkCredential("me.mail", "password"); 
     SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587); 
     smtp.EnableSsl = true; 
     smtp.UseDefaultCredentials = false; 
     smtp.Credentials = loginInfo; 
     smtp.Send(msg); 

就像我說的,這似乎工作正常。但是當我嘗試發送一些更復雜的HTML在瀏覽器中看起來不錯時,我看起來有點不同。我的html:

<body> 
<table cellspacing="0" cellpadding="0" style="width: 640px;"> 
    <tr> 
    <td colspan="3"><img src=srctop.png /></td> 
    </tr> 
    <tr> 
    <td colspan="3"><img src=srcbellowtop.png /></td> 
    </tr> 
<tr> 
    <td><img src=srcleft.png /></td> 
    <td valign="top"><p>Lorem ipsum</p> 
    </td> 
    <td><img align="right" src=srcright.png style="height:675px;"/></td> 
</tr> 
<tr> 
<td colspan=3><img src=srccontinuous.png /></td> 
</tr> 
<tr> 
<td colspan=3><img src=srcfooter.png /></td> 
</tr> 
</table> 
</body> 

問題是,在這封電子郵件中,我得到了頂部圖片和波峯圖片之間的白色空間。在吼吼扇和左邊的&右圖之間也有白色空間。 居然還有所有圖像之間的白色空間:S

任何想法是什麼原因造成這一點,如何解決呢?

鏈接:在瀏覽器中的Gmail http://shrani.si/f/22/OJ/1kNF0emE/ingmail.png 在瀏覽器http://shrani.si/f/20/Eu/3PrPlUnw/inbrowser.png

excpect元素:

table[Attributes Style] { 
border-top-width: 0px; 
border-right-width: 0px; 
border-bottom-width: 0px; 
border-left-width: 0px; 
border-spacing: 0px; 
} 
user agent stylesheettable { 
white-space: normal; 
line-height: normal; 
font-weight: normal; 
font-size: medium; 
font-variant: normal; 
font-style: normal; 
color: -webkit-text; 
text-align: -webkit-auto; 
} 
user agent stylesheettable { 
display: table; 
border-collapse: separate; 
border-spacing: 2px; 
border-color: gray; 
} 

,並在Gmail:

element.style { 
width: 640px; 
} 
Matched CSS Rules 
table[Attributes Style] { 
border-top-width: 0px; 
border-right-width: 0px; 
border-bottom-width: 0px; 
border-left-width: 0px; 
border-spacing: 0px; 
} 
user agent stylesheettable { 
display: table; 
border-collapse: separate; 
border-spacing: 2px; 
border-color: gray; 
} 
+0

歡迎來到電子郵件客戶端地獄,唯一的地方是你的結果比在Web瀏覽器中預測的要少。我會開始確保引用src屬性的值引用。 –

+0

這不是'c#'問題這是一個HTML電子郵件格式問題。我會更具體地說明哪些電子郵件客戶端無法使用。從你所說的話來看,這聽起來像是你只是想從你的細胞中取出邊界。 – James

+0

我試圖在Gmail中打開電子郵件...是的,他們被引用... – gabrjan

回答

2

正如你所澄清的問題是在Gmail中發生,您可以做的最好的事情是使用相關開發人員檢查應用於電子郵件的HTML/CSS您計劃支持的各種瀏覽器的工具

IE - How to use F12 Developer Tools to Debug your Webpages

鉻 - Chrome Developer Tools

火狐 - Firebug

這將允許你縮小的問題,並確定它是否是你自己的造型,或者如果它的造型由瀏覽器添加/客戶。你的源

更新

來看,它出現的問題是,您的圖片被瀏覽器視爲inline-block,將它們設置爲display: block應該解決這個問題。

+0

我沒有任何CSS。原始的html顯示在所有瀏覽器中,但電子郵件全部使用空格... – gabrjan

+0

@ gabrjan你可能沒有HTML/CSS ....但電子郵件客戶端,這很可能出現問題。你需要反擊他們的風格才能擺脫這個問題。 – James

+0

@gabrjan基於你的屏幕截圖在桌子上已經嘗試過'border = 0'嗎?甚至CSS'border-collapse:collapse'。 – James

相關問題