mscdex / node-imap

An IMAP client module for node.js.
MIT License
2.14k stars 379 forks source link

Error in IMAP command received by server #771

Open walrus781 opened 5 years ago

walrus781 commented 5 years ago

Error: Error in IMAP command received by server. i'm working with multiple mails, so i'm trying to get all boxes

var config = {
    user: email,    
    password: pass,
    host: host,
    port: 993,
    tls: true,
    tlsOptions: { 
        rejectUnauthorized: false
    },
    authTimeout: 30000,
    debug: console.log
}

let imap = new Imap(config)
imap.connect();
imap.getBoxes(function (err, boxes) {
    if (err) throw err;
    console.log(boxes)
});

that's what debug is consoled

[connection] Connected to host <= ' OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE NAMESPACE LITERAL+ AUTH=PLAIN AUTH=LOGIN] Dovecot ready.' => 'A0 LIST "" ""' <= 'A0 BAD Error in IMAP command received by server.'

walrus781 commented 5 years ago

okey, i'm just stupid, forgot about imap.once()

walrus781 commented 5 years ago

okey, cause im working with multiple email, i'm trying to loop coming data and connect in loop, but somithing gone wrong, so i'm here again my loop

for(var i in json.data){
    try{
        if(json.data[i].settings.email_conf){
            if(json.data[i].settings.email_conf.host){
                                var config = {
                    user: json.data[i].settings.email_conf.login,   
                    password: json.data[i].settings.email_conf.password,
                    host: host1,
                    port: json.data[i].settings.email_conf.incoming_port,
                    tls: false,
                    autotls: 'always',
                    tlsOptions: { 
                        rejectUnauthorized: false
                    },
                    authTimeout: 30000,
                    debug: console.log
                }
                                let imap = new Imap(config)
                await imap.connect();
                await imap.once('ready', async function(){
                    await imap.getBoxes(async function (err, boxes) {
                        if (err) throw err;
                            console.log(boxes)
                        });
                    })
                }
            }
        }catch(e){
            console.log(e);
        }

i've added tls: false, autotls: 'always',, dunno why, but it's works, almost now i'm getting only one box from the loop there's console

[connection] Connected to host
<= '* OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ STARTTLS AUTH=PLAIN AUTH=LOGIN AUTH=DIGEST-MD5 AUTH=CRAM-MD5] Dovecot ready.'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ STARTTLS AUTH=PLAIN AUTH=LOGIN AUTH=DIGEST-MD5 AUTH=CRAM-MD5'
<= 'A0 OK Pre-login capabilities listed, post-login capabilities have more.'
=> 'A1 STARTTLS'
<= 'A1 OK Begin TLS negotiation now.'
=> 'A2 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ AUTH=PLAIN AUTH=LOGIN AUTH=DIGEST-MD5 AUTH=CRAM-MD5'
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
<= 'A2 OK Pre-login capabilities listed, post-login capabilities have more.'
=> 'A3 LOGIN "logged email" "AA235dbo"'
[connection] Connected to host
<= '* CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE SNIPPET=FUZZY LITERAL+ NOTIFY QUOTA'
<= 'A3 OK Logged in'
=> 'A4 NAMESPACE'
<= '* NAMESPACE (("INBOX." ".")) NIL NIL'
<= 'A4 OK Namespace completed (0.001 + 0.000 secs).'
=> 'A5 LIST "" ""'
<= '* LIST (\\Noselect) "." ""'
<= 'A5 OK List completed (0.001 + 0.000 secs).'
=> 'A6 LIST "" "*"'
<= '* LIST (\\HasChildren) "." INBOX'
<= '* LIST (\\HasNoChildren) "." INBOX.Spam'
<= 'A6 OK List completed (0.001 + 0.000 secs).'
{ INBOX:
   { attribs: [ '\\HasChildren' ],
     delimiter: '.',
     children: { Spam: [Object] },
     parent: null } }
=> 'IDLE IDLE'
<= '+ idling'
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Connected to host
[connection] Ended
[connection] Closed
[connection] Ended
[connection] Closed

then it throws Error: Timed out while authenticating with server

mscdex commented 5 years ago

imap doesn't return Promises from its functions, so having await is useless and could be causing problems with expectations?

walrus781 commented 5 years ago

okey, deleted it, is still same(

mscdex commented 5 years ago

Are they connections to the same host?

walrus781 commented 5 years ago

not all, mostly to justhost, but have few gmails

mscdex commented 5 years ago

Also, the tls/autotls settings will depend on the configuration. This is something you should be getting from your host configuration somewhere. Typically port 143 is plain IMAP (no/explicit TLS) and port 993 is encrypted IMAP (implicit TLS). You could set tls/autotls depending on the port number, but in reality any port number could be used with either kind of encryption setup.

walrus781 commented 5 years ago

port 993 is used at all, with tls and autotls I just experimented, checked what I get

walrus781 commented 5 years ago

that's what i'm get when config like this

var config = {
    user: login,    
    password: password,
    host: host,
    port: port,
    tls: true,
    authTimeout: 30000,
    debug: console.log
}
[connection] Error: Error: 19188:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:openssl\ssl\record\ssl3_record.c:252:
Error: 19188:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:openssl\ssl\record\ssl3_record.c:252:

As I understood it, something is wrong with certificates, but I do not know what and how to solve it D:

mscdex commented 5 years ago

Port 993 isn't used at all? Are the ports different for the servers that aren't working?

walrus781 commented 5 years ago

no, port 993 in every email config which i get

walrus781 commented 5 years ago

So, I try to go through all the mails and take unread messages, I get mail configs from a json object and go through it using for ... in. it turns out that the new cycle, a new connection to another mail from the json object. I'm trying to do about. Hope it helps to understand my problem

mscdex commented 5 years ago

Ok, then tls: true should be correct in that case as long as the servers are using that port number for what it's typically designated for (implicit TLS).

mscdex commented 5 years ago

Also, you should avoid using rejectUnauthorized: false as it's not secure. It will allow anyone to MITM your encrypted connections.

walrus781 commented 5 years ago

yeah, i get it now, thx, but it still throws [connection] Error: Error: 16068:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:openssl\ssl\record\ssl3_record.c:252: and i already removed it rejectUnauthorized: false, thx