ASP.NET: Display HTML Email

This post provides ASP.NET code (C#) to display an HTML email using the Chilkat .NET Email component. It will provide a live example, as well as a download link to the sample code and data files.

This example is only concerned with displaying the HTML body of the email, including embedded images. Displaying the text header fields (Subject, From, To, etc.) is trivial and not included in this example.

This example will load a Chilkat.Email object from a .eml file. It doesn’t matter how the Chilkat.Email object is loaded — whether it’s from a POP3 or IMAP server, or from a file. Once it’s loaded, it may be displayed. (Note: If you fetch headers-only from a POP3 or IMAP server, you will not have the full body so it cannot be displayed. You must download the entire email before the body may be displayed.)

The sample .eml may be downloaded from this URL:
http://www.chilkatsoft.com/data/sampleHtmlWithAttach.eml

A live example is available at this URL:
http://www.chilkatsoft.com/displayEmail1.aspx
Click on the “Display Email” button to display the email.

The source code of the live example may be downloaded here:
http://www.chilkatsoft.com/data/AspNetDisplayEmail.zip

This contains two files: DisplayEmail1.aspx, DisplayEmail1.aspx.cs

How it Works

The Chilkat.Email object provides an AspUnpack2 method that unpacks the email’s embedded images (and stylesheets) to temporary files on the web server. It modifies the HTML so that links are relative and point to the temp files, and then returns the HTML in a byte array using the character encoding of the email. In this example, our HTML page for displaying the email is utf-8 because we want to display email in any language. Prior to calling AspUnpack2, the email.Charset property is set to “utf-8” to convert the email to utf-8 (regardless of the original charset of the email). The HTML bytes returned from AspUnpack2 are converted to a String, and this fills the InnerHTML of a “div”, which renders the HTML email.

DisplayEmail1.aspx Source Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisplayEmail1.aspx.cs" Inherits="DisplayEmail1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>Display HTML Email</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Display Email"
            Width="110px" /><br />
        <br />
        <asp:Label ID="LblStatus" runat="server" Text="Label" Width="128px"></asp:Label> </div>
    </form>
    <div style="border: 1px solid; padding=5px;" id="divDbg" runat="server">
Debug Log
    </div>
    <h2>The HTML body of the email:</h2>
    <hr />
    <div id="divEmail" runat="server">
        
    </div>
    <br />
</body>
</html>


DisplayEmail1.aspx.cs Source Code:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class DisplayEmail1 : System.Web.UI.Page
{
    private Chilkat.MailMan m_mailman = new Chilkat.MailMan();

    protected void Page_Load(object sender, EventArgs e)
    {
        bool success = m_mailman.UnlockComponent("Anything for 30-day trial");
        if (!success)
        {
            divDbg.InnerHtml = "Unlock Failed.";
            LblStatus.Text = "Unlock Failed.";
        }
        else
        {
            divDbg.InnerHtml = "Unlock Successful.";
            LblStatus.Text = "Unlock Successful.";
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Chilkat.Email email = new Chilkat.Email();

        // For demonstration purposes, load an email from a .eml file.
        // This is an HTML email with embedded images.
        bool success = email.LoadEml(Server.MapPath("data/sampleHtmlWithAttach.eml"));
        if (!success)
        {
            divDbg.InnerHtml = email.LastErrorHtml;
            LblStatus.Text = "EML Load Failed.";
            return;
        }

        // Set a unique prefix so that all embedded images associated with
        // this email begin with the same prefix.  This makes it easier for your
        // app to cleanup the files later...
        string prefix = "abc_";
        // This is the directory where embedded images will be placed:
        string saveDir = Server.MapPath("data/emailHtmlFiles");
        string relativeUrlPath = "data/emailHtmlFiles";
        bool autoCleanFiles = true;

        // Regardless of the charset of the email, make sure we get utf-8
        // because our HTML page's charset is utf-8:
        email.Charset = "utf-8";

        // Get the utf-8 HTML:
        byte[] htmlBytes = email.AspUnpack2(prefix, saveDir, relativeUrlPath, autoCleanFiles);
        if (htmlBytes.Length == 0)
        {
            divDbg.InnerHtml = email.LastErrorHtml;
            LblStatus.Text = "AspUnpack2 Failed.";
            return;
        }

        // Display the HTML email.
        divEmail.InnerHtml = Encoding.UTF8.GetString(htmlBytes);
        LblStatus.Text = "OK.";
    }
}