mscdex / node-imap

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

Outlook not adding flag SEEN #893

Closed waterdrop01 closed 1 year ago

waterdrop01 commented 1 year ago

Hello!

It's very strange, using imap.addFlags worked so far with most email providers (Gmail, etc...) BUT didn't work with Outlook (https://outlook.live.com / personal email account):

imap.addFlags(seqno, 'SEEN', (addFlagError) => {
    if (addFlagError) throw addFlagError; // <--- NO Errors... but still, I can see in the webapp that the email is not marked as READ
    debug('added SEEN flag to message', seqno);
});

For the record, I followed these steps to create an "App password": Screenshot from 2022-12-23 23-06-55 Screenshot from 2022-12-23 23-07-18 Screenshot from 2022-12-23 23-07-27 Screenshot from 2022-12-23 23-07-35

And here is how I'm connecting:

const imap = new Imap({
  user: MAIL,
  password: PASSWORD,  // <--- the app password
  host: 'outlook.office365.com',
  port: 993,
  tls: true,
  autotls: 'required',
  tlsOptions: { servername: 'outlook.office365.com' },
});

imap.once('ready', () => {
  debug('connection ready');

  imap.openBox('INBOX', false, (error, box) => {
    if (error) throw error;
    debug('box', box);

    main();
  });
});

And here is what the box object looks like:

box {
  name: 'INBOX',
  flags: [
    '\\Seen',
    '\\Answered',
    '\\Flagged',
    '\\Deleted',
    '\\Draft',
    '$MDNSent'
  ],
  readOnly: false,
  uidvalidity: 14,
  uidnext: 2538,
  permFlags: [ '\\Seen', '\\Answered', '\\Flagged', '\\Deleted', '\\Draft' ],
  keywords: [ '$MDNSent' ],
  newKeywords: false,
  persistentUIDs: true,
  nomodseq: false,
  messages: { total: 408, new: 0 }
}

I also tried with setflags but to no avail.

Any idea? Thanks in advance for your help!

mscdex commented 1 year ago

Are you sure seqno is the correct sequence number for the message you're checking and not a UID or something like that?

waterdrop01 commented 1 year ago

Thanks @mscdex for your help!

Pretty sure because I'm currently using the exact same code for 4 different providers. It works perfect for all except for Outlook.

But in doubt I tried to mark as read a sequence of seqno (box object says there are 408 messages):

imap.addFlags(['390:407'], 'SEEN')

And when verifying in the browser webapp, refreshing, not a single mail was marked as read.

Note: I updated the connection code aboce, as I previously pasted nodemailer stuff instead of 'node-imap'. Also double checked that the INBOX connection is in write mode.

mscdex commented 1 year ago

Oh wait, I missed that you're not using the seq namespace when calling addFlags. Without it, it's working with UIDs. Use imap.seq.addFlags() instead.

waterdrop01 commented 1 year ago

Awesome, thanks so much @mscdex, it worked perfectly with seq.addFlags!!!

Curious why it worked so far with imap.addFlags for at least 3 other mail providers ^^ and I'm sure I was manipulating seqno to fetch, and then to process emails:

const f = imap.seq.fetch(newMessageIDs, {
    bodies: '',
    struct: true,
  });
  f.on('message', (message, seqno) => {
    message.on('body', onMailBody(seqno));
  });

Anyway, happy it's working with Outlook now. Closing the issue...