Sample code for 30+ languages & platforms
SQL Server

Convert Embedded HTML Images to Multipart/Related CID References

See more Email Object Examples

Demonstrates how to convert HTML images that are inline/embedded within the HTML to CID references to multipart/related MIME parts.

Note: This example requires Chilkat v9.5.0.78 or greater.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- This example converts the following email with image data embedded directly in the HTML,
    -- to the MIME shown at the bottom of this page.
    -- If the HTML contains multiple embedded images, the call to email.ConvertInlineImages will convert all of them.

    -- MIME-Version: 1.0
    -- Date: Tue, 30 Apr 2019 11:36:34 +0200
    -- Message-ID: <E5DB5F8A65D671FB4822B22F5309411838F2FC93@PC0VDL0W013>
    -- Content-Type: text/html; charset="windows-1252"
    -- Content-Transfer-Encoding: quoted-printable
    -- X-Priority: 3 (Normal)
    -- From: <admin@chilkatsoft.com>
    -- To: Matt <matt@chilkat.io>
    -- Subject: Test
    -- 
    -- <html xmlns=3D"http://www.w3.org/1999/xhtml">
    -- <body><p>
    -- <img src=3D"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEA3ADcAAD/2=
    -- wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoK=
    -- BggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo=
    -- ...
    -- 4zzS3ww+5/8AyR//2Q=3D=3D" width=3D"58" height=3D"66" alt=3D"" style=3D"bord=
    -- er-width:0px;" /></body>
    -- </html>

    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/embedded_html_image.eml'

    EXEC sp_OAMethod @email, 'ConvertInlineImages', @success OUT
    EXEC sp_OAMethod @email, 'GetMime', @sTmp0 OUT
    PRINT @sTmp0

    -- The MIME after moving the image data to a separate MIME sub-part:

    -- MIME-Version: 1.0
    -- Date: Tue, 30 Apr 2019 11:36:34 +0200
    -- Message-ID: <E5DB5F8A65D671FB4822B22F5309411838F2FC93@PC0VDL0W013>
    -- Content-Type: multipart/related; boundary="------------020808080005060904000806"
    -- X-Priority: 3 (Normal)
    -- From: <admin@chilkatsoft.com>
    -- To: Matt <matt@chilkat.io>
    -- Subject: Test
    -- 
    -- --------------020808080005060904000806
    -- Content-Type: text/html; charset=windows-1252
    -- Content-Transfer-Encoding: quoted-printable
    -- 
    -- <html xmlns=3D"http://www.w3.org/1999/xhtml">
    -- </head></head><body><p>
    -- <img src=3D"cid:CID-1f73052c-32a7-4abb-8e9d-0afb58c17eed@CHILKATSLICE" widt=
    -- h=3D"58" height=3D"66" alt=3D"" style=3D"border-width:0px;" /></body>
    -- </html>
    -- 
    -- 
    -- 
    -- --------------020808080005060904000806
    -- Content-Type: image/jpeg; name="image_95EA3073134C.jpeg"
    -- Content-Transfer-Encoding: base64
    -- Content-ID: <CID-1f73052c-32a7-4abb-8e9d-0afb58c17eed@CHILKATSLICE>
    -- 
    -- /9j/4AAQSkZJRgABAQEA3ADcAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYF
    -- BgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoK
    -- ...
    -- 5bmGF7e42pOwUxrFNEQOcLGw6vX2Yv7NfxbWLYfB67s84vrf/wCLoor8r424ZwGYZkqlSUk+VLRr
    -- /Jn2mS8QYzC0Woxi9eqf+aGr+zX8XUyW8K9/+f8Ag/8AjlO/4Zs+Ln/Qqf8Ak/b/APxyiivkXwPl
    -- H88/vj/8ifUR4zzS3ww+5/8AyR//2Q==
    -- 
    -- --------------020808080005060904000806--

    EXEC @hr = sp_OADestroy @email


END
GO