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

Forward an email as an attachment

See more Email Object Examples

Attach the original Email to a new message as a message/rfc822 part. The recipient can open the attached email and read its original headers and body.

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 @original int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @original OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @original, 'LoadEml', @success OUT, 'message.eml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @original, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @original
        RETURN
      END
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OASetProperty @email, 'From', 'Sam Lee <sam@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 @original
        EXEC @hr = sp_OADestroy @email
        RETURN
      END
    EXEC sp_OASetProperty @email, 'Subject', 'Forwarded message'
    EXEC sp_OASetProperty @email, 'Body', 'The original email is attached.'
    EXEC sp_OAMethod @email, 'AttachEmail', @success OUT, @original
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @original
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    EXEC sp_OAMethod @email, 'SaveEml', @success OUT, 'forward-as-attachment.eml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @original
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    PRINT 'Saved forward-as-attachment.eml.'

    EXEC @hr = sp_OADestroy @original
    EXEC @hr = sp_OADestroy @email


END
GO