Sample code for 30+ languages & platforms
SQL Server

Use Relative Paths When Unpacking HTML Email

See more Email Object Examples

Demonstrates the Chilkat Email.UnpackUseRelPaths property, which applies to the UnpackHtml method. When true (the default), the unpacked HTML uses relative paths for links to the related files (images and style sheets) written to disk; when false, absolute paths are used. This example loads an HTML email and unpacks it using relative paths.

Background: "Unpacking" an HTML email means writing its HTML body to a file and its inline images/stylesheets to a subfolder, then rewriting the cid: references so the saved HTML displays correctly in a browser. Relative paths (like parts/logo.png) keep the folder self-contained and portable — you can move or deploy the whole directory and the links still work. Absolute paths point at one fixed location, which is fine for immediate local viewing but breaks if the files move.

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 Email.UnpackUseRelPaths property, which applies to the UnpackHtml
    --  method.  When true (the default), the unpacked HTML uses relative paths for its links
    --  to the related files (images, style sheets).  When false, absolute paths are used.

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

    --  Load an HTML email that contains related images.
    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

    --  Use relative paths in the unpacked HTML.
    EXEC sp_OASetProperty @email, 'UnpackUseRelPaths', 1

    --  Unpack the HTML body and its related files to the filesystem.
    --  Arguments: unpackDir, htmlFilename, partsSubdir.
    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 using relative paths.'

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