React Native
React Native
Share an Existing SSH Tunnel (Socket) with IMAP
See more IMAP Examples
Demonstrates the Chilkat Imap.UseSshTunnel method, which uses an existing SSH tunnel, represented by a Socket object, for IMAP connections. The only argument is the Socket. Call it before Connect. This example opens and authenticates the tunnel on a Socket, then routes IMAP through it.
Background: A
Socket object can open and hold an SSH tunnel that several Chilkat objects share. This is the approach when the tunnel is managed independently of any one protocol object — for example a long-lived tunnel reused across IMAP, POP3, and SMTP sessions — whereas SshOpenTunnel ties the tunnel's lifetime to the Imap object.Chilkat React Native Downloads
import { Imap, Socket } from '@chilkat/react-native'
async function chilkatExample() {
// Demonstrates the Imap.UseSshTunnel method, which uses an existing SSH tunnel (represented by
// a Socket) for IMAP connections. The only argument is the Socket. Call it before Connect.
const imap = new Imap();
// Open and authenticate an SSH tunnel on a Socket object. This tunnel can be shared with
// other Chilkat objects.
const sshTunnel = new Socket();
const sshPort = 22;
try {
await sshTunnel.sshOpenTunnelAsync('ssh.example.com', sshPort);
} catch {
console.log(sshTunnel.lastErrorText);
return;
}
// The SSH password is not hard-coded in source. Insert code here to obtain it from an
// interactive prompt, environment variable, or a secrets vault.
const sshPassword = '';
try {
await sshTunnel.sshAuthenticatePwAsync('sshUser', sshPassword);
} catch {
console.log(sshTunnel.lastErrorText);
return;
}
// Tell the Imap object to use the existing tunnel.
try {
imap.useSshTunnel(sshTunnel);
} catch {
console.log(imap.lastErrorText);
return;
}
// Now connect and log in to the IMAP server through the tunnel.
imap.ssl = true;
imap.port = 993;
try {
await imap.connectAsync('imap.example.com');
} catch {
console.log(imap.lastErrorText);
return;
}
try {
await imap.loginAsync('user@example.com', 'myPassword');
} catch {
console.log(imap.lastErrorText);
return;
}
try {
await imap.disconnectAsync();
} catch {
console.log(imap.lastErrorText);
return;
}
}