What is the best method for formatting email when using System.Net.Mail

☆樱花仙子☆ 提交于 2020-01-04 02:43:10

问题


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.


回答1:


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.




回答2:


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/




回答3:


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>



回答4:


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 previous 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.



来源:https://stackoverflow.com/questions/340250/what-is-the-best-method-for-formatting-email-when-using-system-net-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!