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

Create an email with plain-text and HTML bodies

See more Email Object Examples

Set the From and Reply-To addresses, add a recipient, and provide plain-text and HTML versions of the same body. Save the message as text-and-html.eml.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr 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_OASetProperty @email, 'FromName', 'Sam Lee'
    EXEC sp_OASetProperty @email, 'FromAddress', 'sam@example.com'
    EXEC sp_OASetProperty @email, 'ReplyTo', 'replies@example.com'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Alex Rivera', 'alex@example.net'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END
    EXEC sp_OASetProperty @email, 'Subject', 'Sample message'
    EXEC sp_OASetProperty @email, 'Charset', 'utf-8'
    --  A custom header can carry a reference alongside the standard message fields.
    EXEC sp_OAMethod @email, 'AddHeaderField', NULL, 'X-Reference-ID', 'REF-42'
    EXEC sp_OAMethod @email, 'AddPlainTextAlternativeBody', @success OUT, 'Read more at https://example.com/details.'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END
    EXEC sp_OAMethod @email, 'AddHtmlAlternativeBody', @success OUT, '<p><a href=''https://example.com/details''>Read more</a></p>'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    EXEC sp_OAMethod @email, 'SaveEml', @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

    PRINT 'Saved text-and-html.eml.'

    EXEC @hr = sp_OADestroy @email


END
GO