Sample code for 30+ languages & platforms
SQL Server

Make a Copy of an Email

See more Email Object Examples

Demonstrates the Chilkat Email.MakeCopy method, which copies the entire state of this email into another Email object — message content, recipients, headers, body representations, attachments, and related items. This example copies an email and reads a couple of fields from the copy.

Background: A deep copy gives you an independent duplicate: changes to one object do not affect the other. This is handy for template-and-tweak workflows — build a base message once, copy it per recipient, then vary only the recipient or a few fields — without risk of one send's edits bleeding into the next.

Chilkat SQL Server Downloads

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

    --  Demonstrates the MakeCopy method, which copies the entire state of this email into
    --  another Email object -- message content, recipients, headers, bodies, attachments, and
    --  related items.

    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, 'Subject', 'Original'
    EXEC sp_OASetProperty @email, 'From', 'alice@example.com'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'
    EXEC sp_OASetProperty @email, 'Body', 'Original body.'

    --  Copy the entire email into a new Email object.
    DECLARE @copy int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @copy OUT

    EXEC sp_OAMethod @email, 'MakeCopy', @success OUT, @copy
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @copy
        RETURN
      END


    EXEC sp_OAGetProperty @copy, 'Subject', @sTmp0 OUT
    PRINT 'Copy subject: ' + @sTmp0

    EXEC sp_OAGetProperty @copy, 'NumTo', @iTmp0 OUT
    PRINT 'Copy NumTo: ' + @iTmp0

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @copy


END
GO