2011-04-12 76 views
2

我正在尝试为用户在我的网站上注册时创建一个漂亮的电子邮件,但我无法通过常规方式在图像上显示文本。相反,文本显示在图像下方。C#html电子邮件 - 图像上的文字/文字

我知道我不能使用样式表,我应该坚持CSS 1.4 ..

我有我敢拉伸(通过设置它的宽度和高度)的背景图像,我会喜欢在这张图片上放一些文字。在普通的html中,我可以使用css方法,例如float:left;位置:绝对div在图像上方浮动。但是这不适用于C#代码,因为它在大多数电子邮件客户端中被截断。如果它是写在下面的方式

图像将只显示:

<div style='width: 580px; font-family: Calibri; font-size:13px;'> 

     <img style='height: 45px; width: inherit;' src='http://www.somedomain.com/mail/background.png' /> 
Test 
Test2 
Test3 
</div> 

我尝试一种风格中嵌入上面的代码,它根本不起作用。例如:

<div style='background:url(images/background.png); position: absolute; top:45px; width:550px; padding: 15px;'> SOME TEXT HERE </div> 

OR

<div style='background-image:url(images/background.png); position: absolute; top:45px; width:550px; padding: 15px;'> SOME TEXT HERE </div> 

OR

<table> 
<tr> 
<td style="background-image: url(background.png);">Testing Page</td> 
</tr> 
</table> 

如果你改变 “的背景图像:网址(background.png);”到“背景:红色”它工作,这是混乱!

如何在大多数电子邮件客户端中正常工作? (如Gmail,Hotmail的Outlook中,雷鸟,Windows Mail中,等)

感谢您的帮助,您可以提供:)

回答

0

我讨厌的坏消息,但你不能跨platformly (是一个单词吗?)在HTML电子邮件中有一个背景图像和/或堆叠元素。

+0

除此之外,如果你的发送者是使用Microsoft Outlook作为其电子邮件客户端应用程序,你会是微软的Outlook是使用基于MS Word中有任何问题,因为邮件渲染,而不是浏览器IE中的设置。所以它很可能无法正常显示。 (请参阅此文章:http://blogs.sitepoint.com/microsoft-breaks-html-email-rendering-in-outlook/) – mallows98 2011-04-12 22:17:30

0

您正在尝试使用不受所有主要电子邮件查看者支持的CSS功能。尝试采取看看这里,看看是怎么支持,什么是不:

http://www.campaignmonitor.com/css/

还有,你可能会感兴趣的sitepoint书:

http://www.sitepoint.com/books/htmlemail1/

+0

而且,正如其他人所说的那样,url必须是绝对的,就像您制作它一样在 2011-04-12 22:20:09

+0

我确实使用了整个网址(一个实际的链接),但没有起作用,所以我尝试了从文件夹链接图像的传统方式,工作,但现在我明白为什么第二种方法不适合这种情况。 – Raskaroth 2011-04-12 23:49:44

2

你或许应该检查出所有的您在Outlook 2007中的电子邮件.Microsoft使用MS Word的呈现引擎确实打破了电子邮件中的HTML功能。为此,Outlook 2007无法识别背景图像。以下是MSDN on supported HTML in Outlook。使用outlook作为你的基线,因为它的支持是最基本的。

+1

也适用于您应该使用完全限定URL的图像(例如http://www.yoursite.com/images/image.png – scrappedcola 2011-04-12 22:17:10

+0

我使用的是电子邮件客户端,例如hotmail和gmail,因为并非所有人都使用Microsoft Outlook。涵盖所有的基地 – Raskaroth 2011-04-12 22:31:32

+0

是的,但如果你代码最坏的情况下,你会被覆盖最好的情况 – scrappedcola 2011-04-12 22:32:55

1

您不能使用相对URL(例如“url(background.png)”或“url(images/background.png)”,因为它与电子邮件相关?电子邮件没有文件夹,文件系统等,就像一个web服务器一样

你有两个选择:使用完整的url语法,就像你在第一个例子中做的一样,或者创建一个MimeHtml消息(MHTML)在MHTML中,你可以绑定一个图像与您的电子邮件,并可能能够以您希望的方式引用它。

+0

该图像是一个适当的链接到网络服务器上的图像,而不是像普通文件夹网站,它仍然无法正常工作,问题只是写得很快,道歉。 – Raskaroth 2011-04-12 22:30:01

2

Iv与电子邮件中的html和图像中的图像有类似的问题。我通过使用内联附件解决了图像问题。诀窍是使用内容ID来设置图像。

希望这个代码将有助于:

编辑:宽度和实际图像高度设置似乎没有工作,虽然。 。 。 :/

 string contentID = "Image1.gif".Replace(".", "") + "@test"; 

     // create the INLINE attachment 
     string attachmentPath = Server.MapPath("Images/Image1.gif"); 

     inline = new Attachment(attachmentPath); 
     inline.ContentDisposition.Inline = true; 
     inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline; 
     inline.ContentId = contentID; 
     inline.ContentType.MediaType = "image/gif"; 
     inline.ContentType.Name = Path.GetFileName(attachmentPath); 

     //then add in the message body 
     //stringbuilder to construct the message 
     sb = new StringBuilder(); 

     sb.Append("<div style=\"font-family:Arial\"> Hello World!<br /><br /><img src=\"@@[email protected]@\" alt=\"\" Width=\"250px\" Height=\"250px\"><br /><br /></div>"); 

     //creating the message with from and to and the smpt server connections 
     mail = new MailMessage("[email protected]", "[email protected]"); 
     SmtpServer = new SmtpClient("smtp.gmail.com"); //Add SMTP settings into the Web.Config --> ConfigurationManager.AppSettings["MyCustomId"] 

     mail.Subject = "Testing Emails"; 
     mail.IsBodyHtml = true; 
     mail.Body = sb.ToString(); 
     mail.Attachments.Add(inline); 

     // replace the tag with the correct content ID 
     mail.Body = mail.Body.Replace("@@[email protected]@", "cid:" + contentID);