I'm trying to use the MailListener library in Node.js to connect to an email server and listen for incoming emails. However, I'm encountering an authentication failure issue, even tho the credentials do work. Here's the code I'm using:
const MailListener = require("mail-listener5");
const axios = require("axios");
const arguments = process.argv.slice(2);
var user = arguments[0];
var pass = arguments[1];
if (user.includes("@gmail.com")) {
var imap = "imap.gmail.com";
user = user.replace("@gmail.com", "");
} else if (user.includes("@outlook.com")) {
var imap = "imap-mail.outlook.com";
user = user.replace("@outlook.com", "");
} else {
console.log(" * ERROR message: Invalid email address");
}
console.log(user, pass, imap);
const Listener = new MailListener({
username: user,
password: pass,
host: imap,
port: 993,
tls: true,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX",
searchFilter: [["FROM", "noreply@steampowered.com"], ["SUBJECT", "New Steam Account Email Verification"], ["UNSEEN"]],
markSeen: true,
fetchUnreadOnStart: true,
});
Listener.on("mail", async (mail, seqno, attributes) => {
const regex = /https:\/\/store\.steampowered\.com\/account\/newaccountverification\?stoken=[^&]+&creationid=[^"]+/;
const match = regex.exec(mail.html);
if (match) {
const verificationLink = match[0];
try {
const response = await axios.get(verificationLink);
console.log(" * INFO message: Verification link visited successfully:", verificationLink);
} catch (error) {
console.error(" * ERROR message: Error visiting the verification link:", error.message);
}
} else {
console.log(" * ERROR message: Verification link not found in the email");
}
});
Listener.on("error", (err) => {
console.log(" * ERROR message: ", err);
});
Listener.start();
When I run this code, I get the following error:
ERROR message: Error: Invalid credentials (Failure)
at Connection._resTagged (C:\Users\maxar\node_modules\imap\lib\Connection.js:1502:11)
at Parser.<anonymous> (C:\Users\maxar\node_modules\imap\lib\Connection.js:194:10)
at Parser.emit (node:events:513:28)
at Parser._resTagged (C:\Users\maxar\node_modules\imap\lib\Parser.js:175:10)
at Parser._parse (C:\Users\maxar\node_modules\imap\lib\Parser.js:139:16)
at Parser._tryread (C:\Users\maxar\node_modules\imap\lib\Parser.js:82:15)
at Parser._cbReadable (C:\Users\maxar\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) {
type: 'no',
textCode: 'AUTHENTICATIONFAILED',
source: 'authentication'
}
I dont know what could have caused this problem, maybe it was somethin like a missing app-password or a OAuth problem, I dont know.
I've tried:
This is happening because Less-secure access for POP3/IMAP e-mail clients has been discontinued, you can read about it here.
You can sign in with an application password instead, but to do that you need to enable 2-step verification on the gmail account and then generate a custom application password. See how to generate application password here.
Replacing your normal login password with the generated application password gives you access to your account.