Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// 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.
let ssh = CkoSsh()!
success = ssh.connect(hostname: "ssh.example.com", port: 22)
if success == false {
print("\(ssh.lastErrorText!)")
return
}
success = ssh.authenticatePw(login: "sshUser", password: "sshPassword")
if success == false {
print("\(ssh.lastErrorText!)")
return
}
let mailman = CkoMailMan()!
// Configure the POP3 server connection. These connections will travel through the tunnel.
mailman.mailHost = "pop.example.com"
mailman.mailPort = 995
mailman.popSsl = true
mailman.popUsername = "user@example.com"
mailman.popPassword = "myPassword"
// Tell MailMan to route its connections through the already-connected Ssh object.
success = mailman.useSsh(ssh: ssh)
if success == false {
print("\(mailman.lastErrorText!)")
return
}
// POP3 operations now travel through the shared SSH connection.
var count: Int = mailman.getMailboxCount().intValue
if count < 0 {
print("Failed to get the mailbox count.")
}
else {
print("Mailbox count: \(count)")
}
ssh.disconnect()
}