Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicPythonRubySQL ServerSwiftTclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcel

SQL Server Examples


ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
Firebase
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)

Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(SQL Server) POP3 over Multiple Hop SSH

Demonstrates how to read POP3 email (using TCP or TLS) tunneled through mulitple-hop SSH. The scheme looks like this:

Application => ServerSSH1 => ServerSSH2 => POP3Server

The ConnectThroughSsh and UseSsh methods are added in Chilkat version 9.5.0.55 to accomplish this task.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    --  Important: It is helpful to send the contents of the
    --  ssh.LastErrorText property when requesting support.

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

    --  Any string automatically begins a fully-functional 30-day trial.
    DECLARE @success int
    EXEC sp_OAMethod @ssh1, 'UnlockComponent', @success OUT, 'Anything for 30-day trial'
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh1, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh1
        RETURN
      END

    --  Connect directly to the 1st SSH server.
    EXEC sp_OAMethod @ssh1, 'Connect', @success OUT, 'serverssh1.com', 22
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh1, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh1
        RETURN
      END

    --  Authenticate using login/password:
    EXEC sp_OAMethod @ssh1, 'AuthenticatePw', @success OUT, 'ssh1Login', 'ssh1Password'
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh1, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh1
        RETURN
      END

    --  Connect through the 1st SSH connection to reach a 2nd SSH server.
    --  Note: Any number of SSH connections may be simultaneously tunneled through a single
    --  existing SSH connection.
    DECLARE @ssh2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Ssh', @ssh2 OUT

    EXEC sp_OAMethod @ssh2, 'ConnectThroughSsh', @success OUT, STR(@ssh1), 'serverssh2.com', 22
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh1
        EXEC @hr = sp_OADestroy @ssh2
        RETURN
      END

    --  Authenticate with ssh2...
    EXEC sp_OAMethod @ssh2, 'AuthenticatePw', @success OUT, 'ssh2Login', 'ssh2Password'
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh1
        EXEC @hr = sp_OADestroy @ssh2
        RETURN
      END

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

    --  Any string argument automatically begins the 30-day trial.
    EXEC sp_OAMethod @mailman, 'UnlockComponent', @success OUT, '30-day trial'
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh1
        EXEC @hr = sp_OADestroy @ssh2
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END

    --  Tell the mailman object to connect to the POP3 server though the ssh2 tunnel (which itself is routed through ssh1).
    --  The connection looks like this:  Application => ServerSSH1 => ServerSSH2 => POP3Server
    EXEC sp_OAMethod @mailman, 'UseSsh', @success OUT, STR(@ssh2)
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh1
        EXEC @hr = sp_OADestroy @ssh2
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END

    --  Set the POP3 server's hostname
    EXEC sp_OASetProperty @mailman, 'MailHost', 'pop.someMailServer.com'

    --  Set the POP3 login/password.
    EXEC sp_OASetProperty @mailman, 'PopUsername', 'myLogin'
    EXEC sp_OASetProperty @mailman, 'PopPassword', 'myPassword'

    --  Establish the POP3 connection through the ssh2 tunnel.
    EXEC sp_OAMethod @mailman, 'Pop3BeginSession', @success OUT
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh1
        EXEC @hr = sp_OADestroy @ssh2
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END

    --  Get the number of messages in the mailbox.
    DECLARE @numMessages int
    EXEC sp_OAMethod @mailman, 'GetMailboxCount', @numMessages OUT

    DECLARE @i int

    IF STR(@numMessages) > 0
      BEGIN

        DECLARE @email int

        SELECT @i = 1
        WHILE @i <= STR(@numMessages)
          BEGIN

            EXEC sp_OAMethod @mailman, 'FetchByMsgnum', @email OUT, STR(@i)
            IF STR(@email) Is NULL 
              BEGIN
                EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
                PRINT @sTmp0
                EXEC @hr = sp_OADestroy @ssh1
                EXEC @hr = sp_OADestroy @ssh2
                EXEC @hr = sp_OADestroy @mailman
                RETURN
              END
            ELSE
              BEGIN
                EXEC sp_OAGetProperty @email, 'From', @sTmp0 OUT

                EXEC sp_OAGetProperty @email, 'Subject', @sTmp1 OUT

                PRINT @sTmp0 + ': ' + @sTmp1 + CHAR(10)
                EXEC @hr = sp_OADestroy @email

              END
            SELECT @i = @i + 1
          END

      END

    --  Close the connection with the POP3 server.  (This closes the tunnel through ssh2)
    --  The connection with ssh2 is still alive, and may be used for more connections.
    EXEC sp_OAMethod @mailman, 'Pop3EndSession', @success OUT

    --  Close the connection with ssh2.  (This closes the the tunnel through ssh1.)
    --  The connection with ssh1 is still alive, and may be used for more connections.
    EXEC sp_OAMethod @ssh2, 'Disconnect', NULL

    EXEC sp_OAMethod @ssh1, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @ssh1
    EXEC @hr = sp_OADestroy @ssh2
    EXEC @hr = sp_OADestroy @mailman


END
GO

 

© 2000-2016 Chilkat Software, Inc. All Rights Reserved.