如何在node.js中处理多个imap连接? [英] How can i handle multiple imap connections in node.js?

查看:250
本文介绍了如何在node.js中处理多个imap连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用node.js同时使用imap监视多个电子邮件帐户?

how can i monitor multiple email accounts using imap at the same time using node.js?

我有一个程序可以使用node-imap模块获取单个帐户的通知,并使用邮件解析器获取已解析的电子邮件.

I have a program to get notifications for single account using node-imap module and parsed emails using mail-parser.

var Imap = require('imap'),
    inspect = require('util').inspect;
var MailParser = require('mailparser').MailParser;
var fs = require('fs');
var imap = new Imap(
{
    user: 'any_email_address',
    password: 'password',
    host: 'imap.host.com',
    port: 993,
    tls: true,
    tlsOptions:
    {
        rejectUnauthorized: false
    }
});

function openInbox(cb)
{
    imap.openBox('INBOX', true, cb);
}

var messages = []

imap.once('ready', function ()
{
    openInbox(function (err, box)
    {
        console.log("open")
        if (err) throw err;
        imap.search(['ALL', []], function (err, results)
        {
            if (err) throw err;
            var f = imap.fetch(results,
            {
                bodies: ''
            });

            f.on('message', function (msg, seqno)
            {
                var mailparser = new MailParser()
                msg.on('body', function (stream, info)
                {
                    stream.pipe(mailparser);
                    mailparser.on("end", function (mail)
                    {
                        fs.writeFile('msg-' + seqno + '-body.html', mail.html, function (err)
                        {
                            if (err) throw err;
                            console.log(seqno + 'saved!');
                        });
                    })
                });
                msg.once('end', function ()
                {
                    console.log(seqno + 'Finished');
                });
            });
            f.once('error', function (err)
            {
                console.log('Fetch error: ' + err);
            });
            f.once('end', function ()
            {
                console.log('Done fetching all messages!');
                imap.end();
            });
        });
    });
});

imap.once('error', function (err)
{
    console.log(err);
});

imap.once('end', function ()
{
    console.log('Connection ended');
});

imap.connect();

推荐答案

您必须创建单独的连接以监视多个帐户.

You have to create separate connections to monitor multiple accounts.

这篇关于如何在node.js中处理多个imap连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆