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

Unable to connect with Office365 Server, where as connection with Gmail server was working fine ??

Open saikirankalyanapu opened this issue 8 years ago • 10 comments

Hi mscdex,

Thank you for your support to node community.I'm using your "node-imap" to connect with email servers.

I tried with Gmail everything was as expected :

var mailListener = new MailListener({ username: "[email protected]", password: "password", host: "imap.gmail.com", port: 993, tls: true, tlsOptions: { rejectUnauthorized: false }, mailbox: "INBOX", markSeen: true, connTimeout: 10000, fetchUnreadOnStart: false, attachments: true, attachmentOptions: { directory: "./server/config/attachments/" } });

Using same code for Office365

var mailListener = new MailListener({ username: "[email protected]", password: "password", host: "outlook.office365.com", port: 995, tls: true, tlsOptions: { rejectUnauthorized: false }, mailbox: "INBOX", markSeen: true, connTimeout: 10000, fetchUnreadOnStart: false, attachments: true, attachmentOptions: { directory: "./server/config/attachments/" } });

When I tried to connect with office 365, getting the following error ...

Error: { [Error: Timed out while authenticating with server] source: 'timeout-auth' } imapDisconnected

Please suggest what could be the error ??

Thanks, Sai.

saikirankalyanapu avatar Jul 22 '16 12:07 saikirankalyanapu

Your port number looks wrong for your Office365 account. It should probably be 993 instead of 995.

On an unrelated note, you shouldn't need to set rejectUnauthorized: false in tlsOptions.

mscdex avatar Jul 22 '16 14:07 mscdex

Thank you msdex for your reply that helped me . Now I'm facing similar issue gain on moving my code base to Heroku , where server keep on loging the following error :

{ [Error: Timed out while authenticating with server] source: 'timeout-auth' }

Where as same config /code was working as expected on localhost i.e my local environment ..

OutLook :

mailListener: { username: "[email protected]", password: "123456", host: "outlook.office365.com", port: 993, tls: true, tlsOptions: {rejectUnauthorized: false}, mailbox: "INBOX", markSeen: true, fetchUnreadOnStart: false, attachments: true, attachmentOptions: {directory: "./attachments/"} }

Gmail :

mailListener: { username: "[email protected]", password: "xxx", host: "imap.googlemail.com", port: 993, tls: true, tlsOptions: {rejectUnauthorized: false}, mailbox: "INBOX", markSeen: true, fetchUnreadOnStart: false, attachments: true, attachmentOptions: {directory: "./attachments/"} }

Gmail connection was working fine on both local and heroku environment . Outlook was working in local environment , but on heroku above error was logging .. Please suggest how can I achieve on cloud platforms ..

Thanks.

saikirankalyanapu avatar Jul 27 '16 14:07 saikirankalyanapu

You can try setting debug: console.log to see exactly what is or isn't happening.

mscdex avatar Jul 27 '16 18:07 mscdex

@mscdex I just switched on my debug mode and here are the logs :

2016-08-25T13:22:22.536172+00:00 app[web.1]: <= '* OK The Microsoft Exchange IMAP4 service is ready. [asdfasdfasdfasdfasdfasdfsdfdsf++++++generatedCODE+++++++++asdfasdfasdfasdfasdfasdfsdfdsf]' 2016-08-25T13:22:22.552561+00:00 app[web.1]: => 'A1 LOGIN "[email protected]" "PASSWORDXXX"' 2016-08-25T13:22:27.553738+00:00 app[web.1]: { [Error: Timed out while authenticating with server] source: 'timeout-auth' } 2016-08-25T13:22:27.554212+00:00 app[web.1]: [connection] Closed

manoj529 avatar Aug 25 '16 13:08 manoj529

@mscdex

Out of 10 attempts i received one successful connection and these are the logs for it :

Success One (When set authTimeout:1000) :

2016-08-25T13:41:42.342466+00:00 app[web.1]: <= '* OK The Microsoft Exchange IMAP4 service is ready. [_TOKEN_********]' 2016-08-25T13:41:51.601984+00:00 app[web.1]: <= 'A1 OK LOGIN completed.' 2016-08-25T13:41:51.602705+00:00 app[web.1]: => 'A2 CAPABILITY' 2016-08-25T13:41:51.910294+00:00 app[web.1]: <= ' CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CLIENTACCESSRULES CLIENTNETWORKPRESENCELOCATION BACKENDAUTHENTICATE CHILDREN IDLE NAMESPACE LITERAL+' 2016-08-25T13:41:51.910309+00:00 app[web.1]: <= 'A2 OK CAPABILITY completed.' 2016-08-25T13:41:52.219123+00:00 app[web.1]: <= '* NAMESPACE (("" "/")) NIL NIL' 2016-08-25T13:41:52.219145+00:00 app[web.1]: => 'A4 LIST "" ""' 2016-08-25T13:41:52.525439+00:00 app[web.1]: <= '* LIST (\Noselect \HasChildren) "/" ""' 2016-08-25T13:41:52.526017+00:00 app[web.1]: <= 'A4 OK LIST completed.' 2016-08-25T13:41:52.526541+00:00 app[web.1]: => 'A5 SELECT "INBOX"' 2016-08-25T13:41:52.881799+00:00 app[web.1]: <= '* 40 EXISTS' 2016-08-25T13:41:52.882352+00:00 app[web.1]: <= '* 0 RECENT' 2016-08-25T13:41:53.191969+00:00 app[web.1]: <= '+ IDLE accepted, awaiting DONE command.' 2016-08-25T13:43:14.738116+00:00 app[web.1]: <= '* 1 RECENT' 2016-08-25T13:43:14.738256+00:00 app[web.1]: <= '* 41 EXISTS' 2016-08-25T13:43:15.050042+00:00 app[web.1]: <= 'IDLE OK IDLE completed.' 2016-08-25T13:43:15.050459+00:00 app[web.1]: => 'A6 UID SEARCH UNSEEN' 2016-08-25T13:43:15.358053+00:00 app[web.1]: <= '* SEARCH 2810' 2016-08-25T13:43:15.358420+00:00 app[web.1]: <= 'A6 OK SEARCH completed.' 2016-08-25T13:43:15.674105+00:00 app[web.1]: <= '* 41 FETCH (UID 2810 FLAGS (\Seen \Recent) INTERNALDATE "25-Aug-2016 19:13:13 +0530" BODY[] {1990}' 2016-08-25T13:43:15.744515+00:00 app[web.1]: Mail Received 2016-08-25T13:43:15.927221+00:00 app[web.1]: <= 'A7 OK FETCH completed.' 2016-08-25T13:43:15.931669+00:00 app[web.1]: => 'IDLE IDLE' 2016-08-25T13:43:16.238413+00:00 app[web.1]: <= '+ IDLE accepted, awaiting DONE command.'

When disconnected Logs :

2016-08-25T13:41:03.221069+00:00 app[web.1]: => 'A0 CAPABILITY' 2016-08-25T13:41:03.424257+00:00 app[web.1]: => 'A1 LOGIN "[email protected]" "PASSWORD"' 2016-08-25T13:41:03.421850+00:00 app[web.1]: <= '* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+' 2016-08-25T13:41:13.242486+00:00 app[web.1]: imapDisconnected 2016-08-25T13:41:13.242361+00:00 app[web.1]: [connection] Closed

manoj529 avatar Aug 25 '16 13:08 manoj529

I have the same issue, here is the error:

Error: Timed out while authenticating with server
    at Timeout._onTimeout (/some-project/node_modules/imap/lib/Connection.js:139:17)
    at ontimeout (timers.js:425:11)
    at tryOnTimeout (timers.js:289:5)
    at listOnTimeout (timers.js:252:5)
    at Timer.processTimers (timers.js:212:10)
Emitted 'error' event at:
    at Timeout._onTimeout (/some-project/node_modules/imap/lib/Connection.js:141:12)
    at ontimeout (timers.js:425:11)
    [... lines matching original stack trace ...]
    at Timer.processTimers (timers.js:212:10)

And here is my simple test:

const Imap = require('imap');
const pump = require('pump');

const imap = new Imap({
  host: 'outlook.office365.com',
  port: '993',
  user: '[email protected]',
  password: 'xxxxx'
});

imap.once('ready', () => {
  console.log('READY');
  imap.openBox('INBOX', true, (err, box) => {
    console.log('BOX OPEN', box.messages.total);
    if (err) {
      throw err;
    }
    const fetch = imap.seq.fetch(box.messages.total + ':*', {
      bodies: ['HEADER', 'TEXT']
    });

    fetch.on('message', (msg, seqno) => {
      console.log('MESSAGE FETCH');
      msg.on('body', (msgStream, info) => {
        console.log('STREAM BODY');
        pump(msgStream, process.stdout, (err) => {
          console.log('Finish pumping !');
          if (err) {
            console.log(err);
          }
          imap.end();
        });
      });
    });
  });
});

imap.connect();

OlivierCuyp avatar Feb 22 '19 17:02 OlivierCuyp

@mscdex In my case I just forgot to put the tls param at true ...

OlivierCuyp avatar Feb 22 '19 19:02 OlivierCuyp

@manoj529 did you find a solution ??

javierfuentesm avatar Dec 31 '21 20:12 javierfuentesm

Office365 requires TLS = true. I think this issue can be closed

clouedoc avatar Sep 13 '22 17:09 clouedoc

Error: LOGIN failed. at Connection._resTagged (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Connection.js:1502:11) at Parser. (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Connection.js:194:10) at Parser.emit (node:events:513:28) at Parser._resTagged (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Parser.js:175:10) at Parser._parse (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Parser.js:139:16) at Parser._tryread (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Parser.js:82:15) at Parser.cbReadable (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Parser.js:53:12) at TLSSocket.emit (node:events:513:28) at emitReadable (node:internal/streams/readable:590:12) at process.processTicksAndRejections (node:internal/process/task_queues:81:21) Emitted 'error' event on Connection instance at: at reentry (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Connection.js:1610:12) at Connection.checkCaps (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Connection.js:1635:22) at Connection._resTagged (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Connection.js:1535:22) at Parser. (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Connection.js:194:10) [... lines matching original stack trace ...] at TLSSocket.emit (node:events:513:28) { type: 'no', textCode: undefined, source: 'authentication' } `const imaps = require('imap'); const { convert } = require('html-to-text'); const { emailConfig } = require('../config/constant');

const readMail = async () => { let connection; try { connection = new imaps({ user: emailConfig.EMAIL, password: emailConfig.PASSWORD, host: 'outlook.office365.com', // Outlook IMAP server port: 993, // Outlook IMAP port tls: true, mailbox: "INBOX", markSeen: true, connTimeout: 10000, fetchUnreadOnStart: false, attachments: true, attachmentOptions: { directory: "./server/config/attachments/" } });

connection.once('ready', () => {
  connection.openBox('INBOX', false, (err, box) => {
    if (err) {
      console.error(err);
      return;
    }

    const searchCriteria = ['UNSEEN'];
    const fetchOptions = {
      bodies: ['HEADER', 'TEXT'],
      markSeen: false,
    };

    connection.search(searchCriteria, fetchOptions, (err, results) => {
      if (err) {
        console.error(err);
        return;
      }

      results.forEach((res) => {
        const text = res.parts.filter((part) => {
          return part.which === 'TEXT';
        });
        let emailHTML = text[0].body;
        let emailText = convert(emailHTML);
        console.log(emailText);
      });
      connection.end();
    });
  });
});

connection.connect();

} catch (error) { console.error(error); if (connection) { connection.end(); } } };

module.exports = { readMail, }; ` please give me solution

karthik-0505 avatar Jan 03 '24 13:01 karthik-0505