Async Node.js Example to Read Many Imap Accounts in Parallel

This example demonstrates how to use Chilkat to connect to many IMAP accounts in parallel to do something..

var os = require('os');
if (os.platform() == 'win32') {
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node14-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node14-win64');
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node14-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node14-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node14-linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('@chilkat/ck-node14-macosx');
}

function unlockChilkat() {

    var glob = new chilkat.Global();
    var success = glob.UnlockBundle("Anything for 30-day trial");
    if (success !== true) {
        console.log(glob.LastErrorText);
        return;
    }

}


function chilkatAsyncExample(json) {

    const account = JSON.parse(json);

    var imap = new chilkat.Imap();

    imap.Ssl = true;
    imap.Port = 993;
    var taskConnect = imap.ConnectAsync(account.server);
    taskConnect.UserData = json;

    const disconnectCompleted = (task) => {
        const account = JSON.parse(task.UserData);
        if (task.StatusInt != 7) {
            console.log("Disconnect did not complete.");
            console.log("task status: " + task.Status);
            console.log("account: " + account.email);
            reject('IMAP disconnect error');
        }

        console.log("Success: " + account.email);
    }


    const mailboxSelectCompleted = (task) => {
        const account = JSON.parse(task.UserData);
        if (task.StatusInt != 7) {
            console.log("Mailbox select did not complete.");
            console.log("task status: " + task.Status);
            console.log("account: " + account.email);
            reject('mailbox select error');
        }

        console.log("Folder selected: " + account.email);

        // Disconnect from the IMAP server.
        var taskDisconnect = imap.DisconnectAsync();
        taskDisconnect.UserData = task.UserData;

        success = taskDisconnect.Run(disconnectCompleted);
        if (success != true) {
            console.log(taskDisconnect.LastErrorText);
        }

    }

    const loginCompleted = (task) => {
        const account = JSON.parse(task.UserData);

        if (task.StatusInt != 7) {
            console.log("Login did not complete.");
            console.log("task status: " + task.Status);
            console.log("account: " + account.email);
            reject('login error');
        }

        console.log("Login successful: " + account.email);

        // Select an IMAP folder/mailbox
        var taskSelectMaibox = imap.SelectMailboxAsync("Inbox");
        taskSelectMaibox.UserData = task.UserData;

        success = taskSelectMaibox.Run(mailboxSelectCompleted);
        if (success != true) {
            console.log(taskSelectMaibox.LastErrorText);
        }

    }

    const connectCompleted = (task) => {
        const account = JSON.parse(task.UserData);
        if (task.StatusInt != 7) {
            console.log("Connect did not complete.");
            console.log("task status: " + task.Status);
            console.log("account: " + account.email);
            reject('connection error');
        }

        console.log("Connected: " + account.email);

        var taskLogin = imap.LoginAsync(account.email, account.password);
        taskLogin.UserData = task.UserData;

        success = taskLogin.Run(loginCompleted);
        if (success != true) {
            console.log(taskLogin.LastErrorText);
        }

    }

    success = taskConnect.Run(connectCompleted);
    if (success != true) {
        console.log(taskConnect.LastErrorText);
    }


}



unlockChilkat();

chilkatAsyncExample('{"server":"imap-mail.outlook.com","email":"chilkat@live.com","password":"***"}');
chilkatAsyncExample('{"server":"imap.mail.us-west-2.awsapps.com","email":"qa@chilkatsoft.com","password":"***"}');
// ....

Tags :