Sample code for 30+ languages & platforms
SQL Server

Unpack an HTML Email to Files

See more Email Object Examples

Demonstrates the Chilkat Email.UnpackHtml method, which unpacks an HTML email into an HTML file plus separate related files (images and style sheets). Links in the HTML are rewritten to reference the unpacked files. The arguments are the unpack directory, the HTML filename, and the subdirectory for the related parts. This example loads an HTML email and unpacks it.

Background: An HTML email keeps its images and style sheets inside the message (referenced by cid:), which a browser cannot open directly. Unpacking writes each part out as a real file and rewrites the references into ordinary paths, so the saved HTML displays correctly — useful for archiving a message as a browsable folder or previewing it outside a mail client. The UnpackUseRelPaths property controls whether the rewritten links use relative or absolute paths; for a single self-contained file, see CreateTempMht, and for web serving see AspUnpack.

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 UnpackHtml method, which unpacks an HTML email into an HTML file plus
    --  separate related files (images, style sheets).  Links in the HTML are rewritten to
    --  reference the unpacked files.  Arguments: unpackDir, htmlFilename, partsSubdir.

    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 body and its related files to the filesystem.
    EXEC sp_OAMethod @email, 'UnpackHtml', @success OUT, 'qa_output/unpacked', 'message.html', 'parts'
    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.'

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