0 votes

How do I include the CSS string when using an HTML email?

by

1 Answer

0 votes
 
Best answer

Hi,

Exactly the same way you do in regular html - use <style> tags.

MailBuilder builder = new MailBuilder();
builder.From.Add(new MailBox("alice@mail.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
builder.Subject = "This is HTML test";

builder.Html = @"
<html>
<head>
    <style>
      body {background-color:lightgray}
      h1   {color:blue}
      p    {color:green}
    </style>
</head>

<body>
  <h1>This is a heading</h1>
  <p>This is a paragraph. <img src = 'cid:image1' /> </p>
</body>
</html>
";

// Read attachment from disk...and add it to Visuals collection
MimeData image = builder.AddVisual(@"c:\image.jpg");
image.ContentId = "image1";

IMail email = builder.Create();

using (Smtp smtp = new Smtp())
{
    smtp.Connect("server.example.com");   // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password"); 

    smtp.SendMessage(email);

    smtp.Close();
}
by (282k points)
...