Sample code for 30+ languages & platforms
React Native

Use an Existing Socket SSH Tunnel

See more SMTP Examples

Demonstrates the Chilkat MailMan.UseSshTunnel method, which configures MailMan to use an existing SSH tunnel provided by a Socket object for its SMTP and POP3 connections. This example opens and authenticates a tunnel on a Socket, hands it to MailMan, and then verifies an SMTP login through it.

Background: Sharing one already-established SSH tunnel across objects avoids opening (and authenticating) a separate SSH connection for every component that needs to reach the remote network. Here the tunnel lives on a Socket object, which MailMan then routes its SMTP/POP3 traffic through. It is the Socket-based sibling of UseSsh, which shares an Ssh object instead.

Chilkat React Native Downloads

React Native
import { MailMan, Socket } from '@chilkat/react-native'

async function chilkatExample() {
  let success = false;

  // Demonstrates the MailMan.UseSshTunnel method, which configures MailMan to use an existing
  // SSH tunnel provided by a Socket object for its SMTP and POP3 connections.

  // Open and authenticate the SSH tunnel using a Socket object.
  const sshTunnel = new Socket();
  try {
    await sshTunnel.sshOpenTunnelAsync('ssh.example.com', 22);
  } catch {
    console.log(sshTunnel.lastErrorText);
    return;
  }

  try {
    await sshTunnel.sshAuthenticatePwAsync('sshUser', 'sshPassword');
  } catch {
    console.log(sshTunnel.lastErrorText);
    return;
  }

  const mailman = new MailMan();

  // Configure the SMTP server connection.  These connections will travel through the tunnel.
  mailman.smtpHost = 'smtp.example.com';
  mailman.smtpPort = 465;
  mailman.smtpSsl = true;
  mailman.smtpUsername = 'user@example.com';
  mailman.smtpPassword = 'myPassword';

  // Tell MailMan to route its connections through the existing tunnel.
  try {
    mailman.useSshTunnel(sshTunnel);
  } catch {
    console.log(mailman.lastErrorText);
    return;
  }

  // SMTP operations now travel through the shared SSH tunnel.
  success = await mailman.verifySmtpLoginAsync();
  if (!success) {
    console.log(mailman.lastErrorText);
    return;
  }

  console.log('Authenticated with the SMTP server through the SSH tunnel.');
}