Sample code for 30+ languages & platforms
SQL Server

Add a To Recipient to an Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddTo method, which adds a single direct (To) recipient. The first argument is the friendly display name and the second is the email address. This example adds one To recipient and prints the resulting count.

Background: The To recipients are a message's primary audience — the people it is directly addressed to. Each recipient is stored as a display name plus an email address, which together render in the header as Bob <bob@example.com>. Call AddTo once per recipient, or use AddMultipleTo to add several from a single comma-separated string.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    --  Demonstrates the AddTo method, which adds a single direct (To) recipient.
    --  The 1st argument is the friendly (display) name, and the 2nd is the email address.

    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', 'AddTo example'
    EXEC sp_OASetProperty @email, 'From', 'alice@example.com'

    DECLARE @success int
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'


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

    EXEC @hr = sp_OADestroy @email


END
GO