How to connect to outlook mail using imapflow library on node js

674 views Asked by At

I'm migrating for whatever reason an app that uses node-imap to imapflow, my imapConfig was using oAuth2.0 to connect to the outlook server, the code is like this:

const getImapConfig = (token) => {
  const mailId = '[email protected]';
  const auth2 = Buffer.from(
    [`user=${mailId}`, `auth=Bearer ${token.accessToken}`, '', ''].join('\x01'),
    'utf-8'
  ).toString('base64');
  const imapConfig = {
    xoauth2: auth2,
    host: 'outlook.office365.com',
    port: 993,
    // user: process.env.USER,
    // password: process.env.PASSWORD,
    tls: true,
    debug: console.log,
    authTimeout: 25000,
    connTimeout: 30000,
    tlsOptions: {
      rejectUnauthorized: false,
      servername: 'outlook.office365.com',
    },
  };

  return imapConfig;
};

now my imapflow code is like this:

const getImapConfigForImapFlow = (token) => {
  const mailId = '[email protected]';
  const auth2 = Buffer.from(
    [`user=${mailId}`, `auth=Bearer ${token.accessToken}`, '', ''].join('\x01'),
    'utf-8'
  ).toString('base64');
  const imapConfig = {
    xoauth2: auth2,
    auth: {
      accessToken: auth2,
    },
    host: 'outlook.office365.com',
    port: 993,
    secure: true,
    logger: console.log,
    authTimeout: 25000,
    connTimeout: 30000,
    tls: {
      rejectUnauthorized: false,
    },
  };

  return imapConfig;
};
 const client = new ImapFlow(getImapConfigForImapFlow(token));
 const lock = await client.getMailboxLock('INBOX');

The error message when trying to connect is: "connection not available", how should I setup Imapflopw to connect?

0

There are 0 answers