Sample code for 30+ languages & platforms
SQL Server

Use an Existing Ssh Object as the Tunnel

See more POP3 Examples

Demonstrates the Chilkat MailMan.UseSsh method, which configures MailMan to use an existing SSH tunnel provided by an Ssh object for its SMTP and POP3 connections. This example connects and authenticates an Ssh object separately, hands it to MailMan, and then performs a POP3 operation through it.

Background: Rather than having MailMan open its own tunnel (SshOpenTunnel), you can manage the SSH connection yourself and share it. That is valuable when several Chilkat objects must reach the same remote network — they all ride one authenticated SSH connection instead of each opening its own — and it enables advanced setups such as multi-hop SSH, where the Ssh object is already chained through intermediate servers.

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

    --  Establish the SSH connection separately, using an Ssh object.
    DECLARE @ssh int
    EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

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

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

    --  Configure the POP3 server connection.  These connections will travel through the tunnel.
    EXEC sp_OASetProperty @mailman, 'MailHost', 'pop.example.com'
    EXEC sp_OASetProperty @mailman, 'MailPort', 995
    EXEC sp_OASetProperty @mailman, 'PopSsl', 1
    EXEC sp_OASetProperty @mailman, 'PopUsername', 'user@example.com'
    EXEC sp_OASetProperty @mailman, 'PopPassword', 'myPassword'

    --  Tell MailMan to route its connections through the already-connected Ssh object.
    EXEC sp_OAMethod @mailman, 'UseSsh', @success OUT, @ssh
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END

    --  POP3 operations now travel through the shared SSH connection.
    DECLARE @count int
    EXEC sp_OAMethod @mailman, 'GetMailboxCount', @count OUT
    IF @count < 0
      BEGIN

        PRINT 'Failed to get the mailbox count.'
      END
    ELSE
      BEGIN

        PRINT 'Mailbox count: ' + @count
      END

    EXEC sp_OAMethod @ssh, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @ssh
    EXEC @hr = sp_OADestroy @mailman


END
GO