Sample code for 30+ languages & platforms
SQL Server

Use an Existing Socket SSH Tunnel

See more SMTP Examples

Demonstrates the Chilkat MailMan.UseSshTunnel method, which configures MailMan to use an existing SSH tunnel provided by a Socket object for its SMTP and POP3 connections. This example opens and authenticates a tunnel on a Socket, hands it to MailMan, and then verifies an SMTP login through it.

Background: Sharing one already-established SSH tunnel across objects avoids opening (and authenticating) a separate SSH connection for every component that needs to reach the remote network. Here the tunnel lives on a Socket object, which MailMan then routes its SMTP/POP3 traffic through. It is the Socket-based sibling of UseSsh, which shares an Ssh object instead.

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.UseSshTunnel method, which configures MailMan to use an existing
    --  SSH tunnel provided by a Socket object for its SMTP and POP3 connections.

    --  Open and authenticate the SSH tunnel using a Socket object.
    DECLARE @sshTunnel int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @sshTunnel OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @sshTunnel, 'SshOpenTunnel', @success OUT, 'ssh.example.com', 22
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sshTunnel, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sshTunnel
        RETURN
      END

    EXEC sp_OAMethod @sshTunnel, 'SshAuthenticatePw', @success OUT, 'sshUser', 'sshPassword'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sshTunnel, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sshTunnel
        RETURN
      END

    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT

    --  Configure the SMTP server connection.  These connections will travel through the tunnel.
    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'

    --  Tell MailMan to route its connections through the existing tunnel.
    EXEC sp_OAMethod @mailman, 'UseSshTunnel', @success OUT, @sshTunnel
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sshTunnel
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END

    --  SMTP operations now travel through the shared SSH tunnel.
    EXEC sp_OAMethod @mailman, 'VerifySmtpLogin', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sshTunnel
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END


    PRINT 'Authenticated with the SMTP server through the SSH tunnel.'

    EXEC @hr = sp_OADestroy @sshTunnel
    EXEC @hr = sp_OADestroy @mailman


END
GO