C# & ADO.NET

Send an HTML page or template as an e-mail in ASP.NET (C#)

C# & ADO.NET · · Published 2026-08-12

An order confirmation or a newsletter is an HTML document sent as the body of an e-mail. The robust approach is a template file with placeholders, filled in code, sent with both an HTML and a plain-text version. Grabbing a live page's HTML with a web request looks tempting but brings scripts, external CSS and authentication problems with it.

The template

Save ~/App_Data/Templates/OrderConfirmation.html with inline styles (e-mail clients ignore most <style> blocks and external stylesheets):

<!DOCTYPE html>
<html lang="en">
<body style="margin:0;padding:0;background:#f4f4f4">
  <table width="100%" cellpadding="0" cellspacing="0" style="background:#f4f4f4"><tr><td align="center">
    <table width="600" cellpadding="24" cellspacing="0" style="background:#ffffff;font-family:Arial,sans-serif;font-size:15px;color:#222222">
      <tr><td>
        <img src="cid:logo" alt="Example Shop" width="140" height="40">
        <h1 style="font-size:22px">Thank you, {{Name}}</h1>
        <p>Your order <strong>#{{OrderId}}</strong> of {{Total}} has been received.</p>
        <p><a href="{{OrderUrl}}" style="color:#0e6a70">View your order</a></p>
      </td></tr>
    </table>
  </td></tr></table>
</body>
</html>

Filling and sending

public static void SendOrderConfirmation(Order o, string customerEmail)
{
    string path = HostingEnvironment.MapPath("~/App_Data/Templates/OrderConfirmation.html");
    string html = File.ReadAllText(path)
        .Replace("{{Name}}", HttpUtility.HtmlEncode(o.CustomerName))
        .Replace("{{OrderId}}", o.Id.ToString())
        .Replace("{{Total}}", HttpUtility.HtmlEncode(o.Total.ToString("C", CultureInfo.GetCultureInfo("en-IE"))))
        .Replace("{{OrderUrl}}", HttpUtility.HtmlAttributeEncode("https://www.example.com/orders/" + o.Id));

    string text = $"Thank you, {o.CustomerName}.\r\nYour order #{o.Id} has been received.\r\nhttps://www.example.com/orders/{o.Id}";

    using (var msg = new MailMessage())
    using (var smtp = new SmtpClient())
    {
        msg.To.Add(customerEmail);
        msg.Subject = $"Order #{o.Id} confirmed";

        // plain text first, HTML last: clients pick the last part they can display
        msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, Encoding.UTF8, "text/plain"));
        var htmlView = AlternateView.CreateAlternateViewFromString(html, Encoding.UTF8, "text/html");

        // embedded logo referenced as cid:logo
        var logo = new LinkedResource(HostingEnvironment.MapPath("~/App_Data/Templates/logo.png"), "image/png")
        {
            ContentId = "logo"
        };
        htmlView.LinkedResources.Add(logo);
        msg.AlternateViews.Add(htmlView);

        smtp.Send(msg);
    }
}

Each value is HTML-encoded before insertion, so a customer named <b>Ann</b> cannot inject markup into the message. Embedded images (cid:) display without the recipient allowing remote images; linking to images on your server is the alternative, at the cost of being blocked by default in many clients.

Why not send a live .aspx page?

Downloading your own page with WebClient.DownloadString and mailing the result means a second HTTP request (without the user's login cookie), relative links that break outside the site, scripts that e-mail clients strip, and stylesheets they ignore. Rendering a user control to a string is possible but carries the same CSS issues. A dedicated template is predictable and testable.

Sending tabular data is covered in Send a GridView by e-mail; SMTP settings in Sending test mail through Gmail SMTP.

Testing the result

E-mail clients differ far more than browsers. Before sending to customers, send test messages to accounts in the main webmail services and desktop clients your audience uses, check the plain-text version, and look at the message with images blocked. Keep the template narrow (about 600 pixels), use tables for layout, and avoid background images and web fonts that many clients ignore.

Official documentation