Sample code for 30+ languages & platforms
SQL Server

Unpack an HTML Email to Disk for Web Display

See more Email Object Examples

Demonstrates the Chilkat Email.AspUnpack method, which unpacks an HTML email's body and its related files (images and style sheets) to the filesystem and rewrites the HTML so those parts load as ordinary files. The arguments are prefix (prepended to saved filenames), saveDir (the filesystem directory where files are written), urlPath (the URL base substituted into the rewritten HTML), and cleanFiles (whether to remove previously unpacked files first). Despite the Asp in its name — a historical artifact — it is not limited to classic ASP; it works with any server-side technology (PHP, ASP.NET, Node, Java, Python, etc.). This example loads an HTML email and unpacks it.

Background: An HTML email keeps its images and style sheets inside the message as multipart/related parts referenced by cid: URLs — which a browser cannot fetch directly. "Unpacking" writes each part out as a real file and rewrites the cid: references into normal URLs or paths, making the message displayable outside a mail client. Common reasons to do this include:

  • Web display: showing a received message in a webmail or web app, where the browser loads the saved assets from a URL (this is what AspUnpack targets, keeping the on-disk saveDir separate from the web-facing urlPath).
  • Archiving: storing an email as a self-contained, browsable HTML folder for long-term records or compliance, viewable years later without the original mail client.
  • Embedded viewers: loading the unpacked HTML into a desktop app's built-in browser / WebView control, which renders local files.
  • Format conversion: feeding the HTML to an HTML-to-PDF (or image) renderer, which needs the images and style sheets present as local files.

For purely local viewing with relative paths (rather than a web urlPath), see the companion method UnpackHtml.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the AspUnpack method, which unpacks an HTML email's body and its related
    --  files (images, style sheets) to the filesystem and rewrites the HTML so it can be
    --  displayed in a web application.  Despite the "Asp" name (historical), it is not limited
    --  to classic ASP -- it applies to any server-side technology that serves the HTML.
    --  Arguments: prefix, saveDir, urlPath, cleanFiles.

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'qa_data/eml/html_with_images.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  Unpack the HTML for web display.  "prefix" is prepended to the saved filenames, "saveDir"
    --  is the filesystem directory where files are written, "urlPath" is the URL base used in
    --  the rewritten HTML, and cleanFiles=true removes previously unpacked files first.
    EXEC sp_OAMethod @email, 'AspUnpack', @success OUT, 'msg', 'qa_output/unpacked', '/unpacked', 1
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END


    PRINT 'Unpacked the HTML email to disk for web display.'

    --  Note: Paths such as "qa_data/..." and "qa_output/..." are relative local
    --  filesystem paths, relative to the current working directory of the running application.

    EXEC @hr = sp_OADestroy @email


END
GO