Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS on Email

Has anyone found a good way of embeding CSS in a programatically produced email. The best way I have found is to put the style code into a resource file and call it into the code.

An emample would be

Dim objBuilder
objBuilder = New StringBuilder

objBuilder.Append(Resources.SystemEmail.CSSStyle)
objBuilder.Append("My Styled Email")

Dim _Body As String = objBuilder.ToString()

This would build the body of the email

Is there any way to make a template file for an email or a better way to call a style sheet into one.

The code in my .resx file would be

<STYLE TYPE="text/css">
<!--
body
{

    font-family: Tahoma, Verdana, Arial;
    font-size: 10pt;
    padding: 3px 0px 0px 0px;
    margin: 0px 0px 0px 0px;

}
-->
</STYLE>

And calling this into the string would call this inline

And with the answers below to send the message I would use this

Dim client As New SmtpClient("localhost")
Dim toAddr As New MailAddress(MailRecipients)
Dim fromAddr As New MailAddress(MailFrom)
Dim message As New MailMessage(fromAddress, toAddress)


message.Subject = "The Subject"
message.Body =  _Body 
message.IsBodyHtml = True
message.BodyEncoding = System.Text.Encoding.UTF8

client.Send(message)
like image 746
Paul Avatar asked Mar 27 '09 15:03

Paul


2 Answers

The only way we've been successful is to include the CSS inline. Document Header style CSS inclusion isn't consistent or reliable.

Also, make sure your email headers are set to: Content-type: text/html; charset=ISO-8859-1

EDIT UTF-8 is also a good option. The KEY point of that statement is the Content-type being set to text/html

like image 102
jerebear Avatar answered Oct 25 '22 15:10

jerebear


Here is a comprehensive guide on which css styles are supported by each email client: http://www.campaignmonitor.com/css/

A guide on what you will need to know: http://www.campaignmonitor.com/design-guidelines/

There is also a large collection of free pre-built templates that you can use as a reference: http://www.campaignmonitor.com/templates/

Finally, the same company provides a testing tool that will show you how your message will show up in each email client for a reasonable fee: http://www.campaignmonitor.com/testing/

like image 23
Gdeglin Avatar answered Oct 25 '22 14:10

Gdeglin