views:

397

answers:

4

Hi I'm using System.Net.Mail to send some HTML formatted emails.

What is the correct method for inserting css into the email message?

I know I can apply formatting to each item, but I'ld rather use style sheets..

EDIT I should have mentioned that this is for an internal application, and I expect 99% of users to be using Outlook or other client, but never hotmail or gmail etc.

+3  A: 

I've always found that strictly using HTML 3.0 compatible tags and formatting works best for all email readers and providers.

nevertheless here is a CSS in Email article that may answer your question, you will find solutions to your css problems.

Alexandre Brisebois
+5  A: 

The email clients don't use CSS in a uniform way so it's difficult to use CSS. This article can help - http://www.alistapart.com/articles/cssemail/

Joe R
+2  A: 

This reference is the best one I've found to see what works in what when it comes to CSS in emails.

To be honest though, CSS support is so, so flaky that you're better off doing as the previus poster says and sticking to basic HTML (and yes, that means using outdated font tags etc). Especially as you mention Outlook and that (at least the latest version) is the worst culprit when it comes to terrible CSS support.

Mark B
Great resource dude, thanks
DrG
+2  A: 

You could keep styles in a normal CSS file, load them with File.ReadAllText and insert them to a HTML letter template

string letter = File.ReadAllText(Request.PhysicalApplicationPath + "letter.html");
string style = File.ReadAllText(Request.PhysicalApplicationPath + "style.css");

MailMessage message = new MailMessage();
message.Body = letter.Replace("{STYLE}", style);

The letter template should contain {STYLE} in appropriate place

<head>
    <title></title>
    <style type="text/css">
        {STYLE}
    </style>
</head>
<body>
Alexander Prokofyev