Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recent and New not working for outlook account #900

Open
MadhabaPatra opened this issue Mar 21, 2023 · 2 comments
Open

Recent and New not working for outlook account #900

MadhabaPatra opened this issue Mar 21, 2023 · 2 comments

Comments

@MadhabaPatra
Copy link

I am using node-imap to search for the latest emails in my Outlook account's inbox. The documentation mentions the use of the "recent" or "new" flag. However, it seems to work fine for Gmail but not for Outlook. I have attached my code below. Can someone please suggest a solution?

var Imap = require("imap"); // version :  "^0.8.19",

let config = {
  keepalive: {
    interval: 4000,
    idleInterval: 200000,
    forceNoop: true,
  },
  user: "example@outlook.com",
  password: "xxxxxxxxxxx", // app password
  host: "outlook.office365.com",
  port: 993,
  tls: true,
  tlsOptions: { rejectUnauthorized: false },
};

let imap = new Imap(config);

imap.connect()

imap.once("ready", function () {
  console.log("Email connection established!");

  imap.openBox("INBOX", false, function (err, box) {
    if (err) {
      console.log("Error opening inbox");
    }
  });
});

imap.on("error", function (err) {
  console.log(err);
});

imap.on("close", function (err) {
  console.log("connection closed");
});

imap.on("end", function (err) {
  console.log("connection ended");
});

imap.on("mail", function (newMsgNums) {
  console.log(`${newMsgNums} mails received!`);

 imap.search(["RECENT"], function (error, results) {
    if (!results || results.length == 0) {
      console.log(`No recent email available!`);
      return;
    }

    var fetchQuery = imap.fetch(results, {
      bodies: [""],
      struct: true,
    });

    fetchQuery.on("message", function (msg, seqno) {
      msg.on("body", async function (stream, info) {
        console.log(stream);
      });
      msg.on("attributes", function (attrs) {
        console.log(attrs);
      });
      msg.on("end", function () {});
    });

    fetchQuery.once("error", function (err) {
      console.log(err);
    });
    fetchQuery.on("end", function () {});
  });
});

So here on mail event triggered when mails arrive , and i am searching recent mails on those mails.

@erivalo
Copy link

erivalo commented Mar 21, 2023

@MadhabaPatra
Copy link
Author

@erivalo

I understand that when it comes to user authentication within an organization, there can be concerns around the scope for a user (like please contact administrators). To address this, I am opting to use Outlook with an app password to use imap service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants