Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

Create an HTML preview with embedded images

See more Email Object Examples

Make an independent copy of with-attachments.eml, remove ordinary file attachments from the copy, and export its HTML with related images as data URLs. The original retains its attachments. Use a sanitized viewer when showing untrusted HTML.

Chilkat SQL Server Downloads

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

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.

    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, 'with-attachments.eml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END
    DECLARE @preview int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @preview OUT

    EXEC sp_OAMethod @email, 'MakeCopy', @success OUT, @preview
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @preview
        RETURN
      END
    --  Keep related items: the HTML still refers to them.
    EXEC sp_OAMethod @preview, 'DropAttachments', NULL

    EXEC sp_OAGetProperty @email, 'NumAttachments', @iTmp0 OUT

    EXEC sp_OAGetProperty @preview, 'NumAttachments', @iTmp1 OUT
    PRINT 'Files in original: ' + @iTmp0 + '  Files in preview: ' + @iTmp1
    EXEC sp_OAMethod @preview, 'HasHtmlBody', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'This message has no HTML preview.'
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @preview
        RETURN
      END
    DECLARE @html int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @html OUT

    EXEC sp_OAMethod @preview, 'GetHtmlBodySb', @success OUT, 1, @html
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @preview, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @preview
        EXEC @hr = sp_OADestroy @html
        RETURN
      END
    EXEC sp_OAMethod @html, 'WriteFile', @success OUT, 'preview.html', 'utf-8', 0
    IF @success <> 1
      BEGIN

        PRINT 'Failed: html.WriteFile.'
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @preview
        EXEC @hr = sp_OADestroy @html
        RETURN
      END

    PRINT 'Saved preview.html.'

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @preview
    EXEC @hr = sp_OADestroy @html


END
GO