SQL Server Stored Procedure Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

SQL Server
Stored Procedure Examples

Quick Start
Encryption
File Access
IMAP
POP3
SMTP
Email Object
DKIM / DomainKey
FTP
HTML Conversion
HTTP
MHT
MIME
NTLM
RSA
Diffie-Hellman
DSA
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
String
Tar
Upload
XML
XMP
Zip

Amazon S3
Bz2
CSV
FileAccess
Byte Array
RSS
Atom
Self-Extractor

POP3 SSH Tunneling (Port Forwarding)

Demonstrates how to connect to a POP3 server through an SSH tunnel. Reads a POP3 mailbox and display the FROM and SUBJECT header fields of each email.

Download Chilkat Email ActiveX

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    --  The mailman object is used for receiving (POP3)
    --  and sending (SMTP) email.
    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan2', @mailman OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Any string argument automatically begins the 30-day trial.
    DECLARE @success int

    EXEC sp_OAMethod @mailman, 'UnlockComponent', @success OUT, '30-day trial'
    IF @success <> 1
      BEGIN
        PRINT 'Component unlock failed'
        RETURN
      END

    --  Connect to an SSH server and establish the SSH tunnel:
    DECLARE @sshHostname nvarchar(4000)

    DECLARE @sshPort int

    DECLARE @bForSmtp int

    --  The SSH hostname may be a hostname or an
    --  IP address, such as "192.168.1.108".
    --  The port is typically 22 (the standard port for SSH).
    SELECT @sshHostname = 'www.my-ssh-server.com'
    SELECT @sshPort = 22
    --  Select POP3 by setting the following variable to 0
    SELECT @bForSmtp = 0
    --  The 1st argument passed to SshTunnel indicates whether
    --  a tunnel for SMTP or POP3 is being established.
    EXEC sp_OAMethod @mailman, 'SshTunnel', @success OUT, @bForSmtp, @sshHostname, @sshPort
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  Authenticate with the SSH server via a login/password
    --  or with a public key.
    --  This example demonstrates SSH password authentication.
    --  Note: This is not authenticating with the POP3 server, it is
    --  for authenticating with the SSH server, which is separate.
    EXEC sp_OAMethod @mailman, 'SshAuthenticatePw', @success OUT, @bForSmtp, 'ssh_login', 'ssh_password'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  OK, the SSH tunnel is setup.  The mailman may
    --  be used exactly the same as usual, except all communications
    --  are now sent through the SSH tunnel.

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

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

    --  Download a max of 5 messages
    --  To download all email, leave MaxCount unset so it remains at the default value of 0.
    EXEC sp_OASetProperty @mailman, 'MaxCount', 5

    DECLARE @bundle int

    --  Copy the all email from the user's POP3 mailbox
    --  into a bundle object.  The email remains on the server.
    EXEC sp_OAMethod @mailman, 'CopyMail', @bundle OUT

    --  If you're curious, have a look at LastErrorText after
    --  a successful send.  You'll see detailed information that
    --  confirms the SSH tunneling.
    EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
    PRINT @sTmp0

    IF @bundle Is NULL 
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    DECLARE @i int

    DECLARE @email int

    EXEC sp_OAGetProperty @bundle, 'MessageCount', @iTmp0 OUT
    SELECT @i = 0
    WHILE @i <= @iTmp0 - 1
      BEGIN
        EXEC sp_OAMethod @bundle, 'GetEmail', @email OUT, @i
        EXEC sp_OAGetProperty @email, 'From', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @email, 'Subject', @sTmp0 OUT


        PRINT @sTmp0 + CHAR(13)+CHAR(10)

        SELECT @i = @i + 1
      END

    --  Close the connection (with both SSH server and POP3 server)
    EXEC sp_OAMethod @mailman, 'Pop3EndSession', NULL
END
GO

 

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