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

Add an attachment and an inline image

See more Email Object Examples

Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.

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
    DECLARE @iTmp0 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, 'text-and-html.eml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END
    DECLARE @data int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @data OUT

    EXEC sp_OAMethod @data, 'AppendString', @success OUT, 'Time,Activity' + CHAR(13) + CHAR(10) + '09:00,Welcome' + CHAR(13) + CHAR(10) + '10:00,Field session' + CHAR(13) + CHAR(10), 'utf-8'
    IF @success <> 1
      BEGIN

        PRINT 'Failed: data.AppendString.'
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @data
        RETURN
      END
    EXEC sp_OAMethod @email, 'AddAttachmentBd', @success OUT, 'agenda.csv', @data, 'text/csv'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @data
        RETURN
      END

    --  AddRelatedBd returns the identifier that must appear after cid: in the HTML.
    DECLARE @image int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @image OUT

    EXEC sp_OAMethod @image, 'AppendEncoded', @success OUT, 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==', 'base64'
    IF @success <> 1
      BEGIN

        PRINT 'Failed: image.AppendEncoded.'
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @data
        EXEC @hr = sp_OADestroy @image
        RETURN
      END
    DECLARE @cid nvarchar(4000)
    EXEC sp_OAMethod @email, 'AddRelatedBd', @cid OUT, 'confirmed.png', @image
    EXEC sp_OAGetProperty @email, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @data
        EXEC @hr = sp_OADestroy @image
        RETURN
      END
    DECLARE @html int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @html OUT

    EXEC sp_OAMethod @html, 'Append', @success OUT, '<p><img alt=''Confirmed'' src="cid:'
    EXEC sp_OAMethod @html, 'Append', @success OUT, @cid
    EXEC sp_OAMethod @html, 'Append', @success OUT, '"></p>'
    EXEC sp_OAMethod @email, 'GetHtmlBody', @sTmp0 OUT
    EXEC sp_OAMethod @html, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @html, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @email, 'AddHtmlAlternativeBody', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @data
        EXEC @hr = sp_OADestroy @image
        EXEC @hr = sp_OADestroy @html
        RETURN
      END

    EXEC sp_OAMethod @email, 'SaveEml', @success OUT, 'with-attachments.eml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @data
        EXEC @hr = sp_OADestroy @image
        EXEC @hr = sp_OADestroy @html
        RETURN
      END

    PRINT 'Saved with-attachments.eml.'

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @data
    EXEC @hr = sp_OADestroy @image
    EXEC @hr = sp_OADestroy @html


END
GO