Sample code for 30+ languages & platforms
SQL Server

Explicitly Connect to the SMTP Server

See more SMTP Examples

Demonstrates the Chilkat MailMan.SmtpConnect method, which explicitly connects to the SMTP server. If SSL/TLS is required by the current property settings, the secure TLS channel is established as part of connecting. This example opens the connection.

Background: SmtpConnect performs just the connect (and TLS handshake) step, without authenticating — useful when you want fine-grained control over the connection lifecycle, or to separate connecting from authenticating (SmtpAuthenticate) for diagnostics. For everyday sending you don't need to call it: SendEmail connects automatically.

Chilkat SQL Server Downloads

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

    --  Demonstrates the MailMan.SmtpConnect method, which explicitly connects to the SMTP server.
    --  If SSL/TLS is required by the current property settings, the secure channel is established
    --  as part of connecting.

    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

    --  Explicitly connect to the SMTP server (does not authenticate).

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


    PRINT 'Connected to the SMTP server.'

    EXEC @hr = sp_OADestroy @mailman


END
GO