Sample code for 30+ languages & platforms
SQL Server

Clear an Email Object

See more Email Object Examples

Demonstrates the Chilkat Email.Clear method, which removes the current message content — recipients, headers, body representations, attachments, related items, and embedded messages — leaving an empty email object. This example builds a message with a recipient and attachment, clears it, and prints the counts before and after.

Background: Reusing a single Email object across many operations is efficient, but leftover state from a previous message could leak into the next. Clear resets the object completely, giving you a clean slate — the safe way to start a fresh message without allocating a new object.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    --  Demonstrates the Clear method, which removes the current message content, including
    --  recipients, headers, body representations, attachments, related items, and embedded
    --  messages -- leaving an empty email object.

    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', 'A message'
    EXEC sp_OASetProperty @email, 'Body', 'Some body text.'
    DECLARE @success int
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'
    EXEC sp_OAMethod @email, 'AddStringAttachment', @success OUT, 'notes.txt', 'Some notes.'


    EXEC sp_OAGetProperty @email, 'NumTo', @iTmp0 OUT
    PRINT 'NumTo before clear = ' + @iTmp0

    EXEC sp_OAGetProperty @email, 'NumAttachments', @iTmp0 OUT
    PRINT 'NumAttachments before clear = ' + @iTmp0

    --  Remove all content, resetting the email object.
    EXEC sp_OAMethod @email, 'Clear', NULL


    EXEC sp_OAGetProperty @email, 'NumTo', @iTmp0 OUT
    PRINT 'NumTo after clear = ' + @iTmp0

    EXEC sp_OAGetProperty @email, 'NumAttachments', @iTmp0 OUT
    PRINT 'NumAttachments after clear = ' + @iTmp0

    EXEC @hr = sp_OADestroy @email


END
GO