Sample code for 30+ languages & platforms
SQL Server

Send a Bundle of Emails via SMTP

See more SMTP Examples

Demonstrates the Chilkat MailMan.SendBundle method, which sends each email in an EmailBundle. This is equivalent to calling SendEmail once for each email in the bundle. This example builds a bundle of two messages and sends them.

Background: Sending a bundle lets Chilkat reuse a single SMTP connection for all the messages, avoiding the connect/authenticate overhead of sending them one at a time — the same efficiency as the explicit open/send-loop/close pattern, in one call. It is handy for dispatching a queued batch of distinct messages (as opposed to one message to many recipients).

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 MailMan.SendBundle method, which sends each email in an EmailBundle.
    --  This is equivalent to calling SendEmail once for each email in the bundle.

    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Configure the SMTP server connection.
    EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.example.com'
    EXEC sp_OASetProperty @mailman, 'SmtpPort', 465
    EXEC sp_OASetProperty @mailman, 'SmtpSsl', 1
    EXEC sp_OASetProperty @mailman, 'SmtpUsername', 'user@example.com'
    EXEC sp_OASetProperty @mailman, 'SmtpPassword', 'myPassword'

    --  Build a bundle of emails to send.
    DECLARE @bundle int
    EXEC @hr = sp_OACreate 'Chilkat.EmailBundle', @bundle OUT

    DECLARE @email1 int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email1 OUT

    EXEC sp_OASetProperty @email1, 'Subject', 'Message 1'
    EXEC sp_OASetProperty @email1, 'From', 'alice@example.com'
    EXEC sp_OAMethod @email1, 'AddTo', @success OUT, 'Bob', 'bob@example.com'
    EXEC sp_OASetProperty @email1, 'Body', 'This is the first message.'
    EXEC sp_OAMethod @bundle, 'AddEmail', @success OUT, @email1

    DECLARE @email2 int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email2 OUT

    EXEC sp_OASetProperty @email2, 'Subject', 'Message 2'
    EXEC sp_OASetProperty @email2, 'From', 'alice@example.com'
    EXEC sp_OAMethod @email2, 'AddTo', @success OUT, 'Carol', 'carol@example.com'
    EXEC sp_OASetProperty @email2, 'Body', 'This is the second message.'
    EXEC sp_OAMethod @bundle, 'AddEmail', @success OUT, @email2

    --  Send every email in the bundle.

    EXEC sp_OAMethod @mailman, 'SendBundle', @success OUT, @bundle
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @bundle
        EXEC @hr = sp_OADestroy @email1
        EXEC @hr = sp_OADestroy @email2
        RETURN
      END


    EXEC sp_OAGetProperty @bundle, 'MessageCount', @iTmp0 OUT

    PRINT 'Sent ' + @iTmp0 + ' emails.'

    --  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
    --  connects and authenticates -- using the property settings above -- whenever a server
    --  operation requires it.  Calling the explicit connect/authenticate methods can still be
    --  helpful to determine whether a failure occurs while connecting or while authenticating.

    EXEC @hr = sp_OADestroy @mailman
    EXEC @hr = sp_OADestroy @bundle
    EXEC @hr = sp_OADestroy @email1
    EXEC @hr = sp_OADestroy @email2


END
GO