node-imap icon indicating copy to clipboard operation
node-imap copied to clipboard

Fetch error: Error: The specified message set is invalid. !!

Open ahmedisam99 opened this issue 7 years ago • 8 comments

I am trying the following code to fetch Hotmail/outlook emails :

var Imap = require("imap"),
  inspect = require("util").inspect;

var imap = new Imap({
  user: "[email protected]",
  password: "mypassword",
  host: "imap-mail.outlook.com",
  port: 993,
  tls: true
});

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

imap.once("ready", function() {
  openInbox(function(err, box) {
    if (err) throw err;
    var f = imap.seq.fetch(`*`, {
      bodies: "HEADER.FIELDS (FROM TO SUBJECT DATE)",
      struct: true
    });
    f.on("message", function(msg, seqno) {
      console.log("Message #%d", seqno);
      var prefix = "(#" + seqno + ") ";
      msg.on("body", function(stream, info) {
        var buffer = "";
        stream.on("data", function(chunk) {
          buffer += chunk.toString("utf8");
        });
        stream.once("end", function() {
          console.log(
            prefix + "Parsed header: %s",
            inspect(Imap.parseHeader(buffer))
          );
        });
      });
      msg.once("attributes", function(attrs) {
        console.log(prefix + "Attributes: %s", inspect(attrs, false, 8));
      });
      msg.once("end", function() {
        console.log(prefix + "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();

But, I'm getting the following error :

Fetch error: Error: The specified message set is invalid.
Done fetching all messages!
Connection ended

@mscdex please help !!

ahmedisam99 avatar Apr 12 '19 11:04 ahmedisam99

Is the mailbox empty?

mscdex avatar Apr 12 '19 13:04 mscdex

@mscdex no it's not empty

ahmedisam99 avatar Apr 12 '19 14:04 ahmedisam99

Do you get the same results if you use imap.fetch()? If so, can you set debug: console.log in your connection config object and post the output here (removing username and password from the resulting debug output)?

mscdex avatar Apr 12 '19 15:04 mscdex

@mscdex I did add it

var imap = new Imap({
  user: "[email protected]",
  password: "mypass",
  host: "imap-mail.outlook.com",
  port: 993,
  tls: true,
  debug: console.log
});

And got the following :

[connection] Connected to host
<= '* OK The Microsoft Exchange IMAP4 service is ready. [RABCADYAUABSADAAMQBDAEEAMAAwADMAMwAuAGUAdQByAHAAcgBkADAAMQAuAHAAcgBvAGQALgBlAHgAYwBoAGEAbgBnAGUAbABhAGIAcwAuAGMAbwBtAA==]'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+'
<= 'A0 OK CAPABILITY completed.'
=> 'A1 LOGIN "USERNAME" "PASSWORD"'
<= 'A1 OK LOGIN completed.'
=> 'A2 CAPABILITY'
<= '* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CLIENTACCESSRULES CLIENTNETWORKPRESENCELOCATION BACKENDAUTHENTICATE CHILDREN IDLE NAMESPACE LITERAL+'
<= 'A2 OK CAPABILITY completed.'
=> 'A3 NAMESPACE'
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A3 OK NAMESPACE completed.'
=> 'A4 LIST "" ""'
<= '* LIST (\\Noselect \\HasChildren) "/" ""'
<= 'A4 OK LIST completed.'
=> 'A5 EXAMINE "INBOX"'
<= '* 4 EXISTS'
<= '* 3 RECENT'
<= '* FLAGS (\\Seen \\Answered \\Flagged \\Deleted \\Draft $MDNSent)'
<= '* OK [PERMANENTFLAGS ()] Permanent flags'
<= '* OK [UNSEEN 2] Is the first unseen message'
<= '* OK [UIDVALIDITY 14] UIDVALIDITY value'
<= '* OK [UIDNEXT 6726] The next unique identifier value'
<= 'A5 OK [READ-ONLY] EXAMINE completed.'
=> 'A6 FETCH * (UID FLAGS INTERNALDATE BODYSTRUCTURE BODY.PEEK[HEADER.FIELDS (FROM TO SUBJECT DATE CC IN-REPLY)])'
<= 'A6 BAD The specified message set is invalid.'
Fetch error: Error: The specified message set is invalid.
=> 'IDLE IDLE'
Done fetching all messages!
<= '+ IDLE accepted, awaiting DONE command.'
=> DONE
<= 'IDLE OK IDLE completed.'
=> 'A7 LOGOUT'
<= '* BYE Microsoft Exchange Server IMAP4 server signing off.'
<= 'A7 OK LOGOUT completed.'
[connection] Ended
Connection ended
[connection] Closed

ahmedisam99 avatar Apr 14 '19 07:04 ahmedisam99

@mscdex The problem is with the fetch bodies option, I have tried "HEADER", "", "HEADER.FIELDs" and none worked

ahmedisam99 avatar Apr 14 '19 07:04 ahmedisam99

@mscdex Done, the problem was with the source range, I used imap.seq.fetch("*", options) and worked with gmail but it didn't work with hotmail/outlook so i changed it to "1:*" Relates #615

ahmedisam99 avatar Apr 14 '19 08:04 ahmedisam99

Ah ok, yeah not all server IMAP implementations behave correctly, especially outlook/exchange from what I've seen in the past.

mscdex avatar Apr 14 '19 14:04 mscdex

Ironically, it is Microsoft Exchange that behaved correctly in this particular case and GMail that did not follow the spec.

* really is a bad sequence range. See the grammar definitions in rfc3501, Page 90:

seq-number      = nz-number / "*"
                    ; message sequence number (COPY, FETCH, STORE
                    ; commands) or unique identifier (UID COPY,
                    ; UID FETCH, UID STORE commands).
                    ; * represents the largest number in use.  In
                    ; the case of message sequence numbers, it is
                    ; the number of messages in a non-empty mailbox.
                    ; In the case of unique identifiers, it is the
                    ; unique identifier of the last message in the
                    ; mailbox or, if the mailbox is empty, the
                    ; mailbox's current UIDNEXT value.
                    ; The server should respond with a tagged BAD
                    ; response to a command that uses a message
                    ; sequence number greater than the number of
                    ; messages in the selected mailbox.  This
                    ; includes "*" if the selected mailbox is empty.

seq-range       = seq-number ":" seq-number
                    ; two seq-number values and all values between
                    ; these two regardless of order.
                    ; Example: 2:4 and 4:2 are equivalent and indicate
                    ; values 2, 3, and 4.
                    ; Example: a unique identifier sequence range of
                    ; 3291:* includes the UID of the last message in
                    ; the mailbox, even if that value is less than 3291.

jstedfast avatar Jan 25 '24 15:01 jstedfast