Sample code for 30+ languages & platforms
PureBasic

Authenticate an SSH Tunnel with a Password

See more SMTP Examples

Demonstrates the Chilkat MailMan.SshAuthenticatePw method, which authenticates with the SSH server using an SSH username and password after an SSH tunnel has been opened. The first argument is the SSH account name and the second is the corresponding password. This example opens a tunnel, authenticates, and sends SMTP traffic through it.

Background: Opening an SSH tunnel and authenticating to the SSH server are two separate steps, and note the SSH credentials are distinct from the mail account credentials — the SSH login gets you through the tunnel, while SmtpUsername/SmtpPassword authenticate to the mail server at the far end. Password authentication is the simplest option; public-key authentication (SshAuthenticatePk) is generally preferred for automated systems.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the MailMan.SshAuthenticatePw method, which authenticates with the SSH server
    ;  using an SSH username and password after an SSH tunnel has been opened.  The 1st argument
    ;  is the SSH account name and the 2nd is the corresponding password.

    mailman.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Configure the SMTP server connection.  These connections will travel through the tunnel.
    CkMailMan::setCkSmtpHost(mailman, "smtp.example.com")
    CkMailMan::setCkSmtpPort(mailman, 465)
    CkMailMan::setCkSmtpSsl(mailman, 1)
    CkMailMan::setCkSmtpUsername(mailman, "user@example.com")
    CkMailMan::setCkSmtpPassword(mailman, "myPassword")

    ;  Open the SSH tunnel first.
    success = CkMailMan::ckSshOpenTunnel(mailman,"ssh.example.com",22)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    ;  Authenticate with the SSH server using a username and password.
    success = CkMailMan::ckSshAuthenticatePw(mailman,"sshUser","sshPassword")
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    Debug "SSH tunnel authenticated."

    ;  SMTP operations now travel through the SSH tunnel.
    success = CkMailMan::ckVerifySmtpLogin(mailman)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    success = CkMailMan::ckSshCloseTunnel(mailman)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf



    CkMailMan::ckDispose(mailman)


    ProcedureReturn
EndProcedure