Reputation: 77
I am using IMAP library to fetch email from gmail. I got reference from Read email body with node js imap and https://www.npmjs.com/package/imap.
My implementation is as following:
var Imap = require('imap'),
inspect = require('util').inspect;
var imap = new Imap({
user: '[email protected]',
password: 'xxxxxx',
host: 'imap.gmail.com',
port: 993,
tls: true
});
function openInbox(cb) {
imap.openBox('INBOX', true, cb);
}
imap.once('ready', function() {
openInbox(function(err, box) {
if (err) throw err;
var f = imap.seq.fetch('1:3', {
bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
struct: true
});
f.on('message', function(msg, seqno) {
console.log('Message #%d', seqno);
var prefix = '(#' + seqno + ') ';
msg.on('body', function(stream, info) {
var buffer = '';
stream.on('data', function(chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function() {
console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
});
});
msg.once('attributes', function(attrs) {
console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
});
msg.once('end', function() {
console.log(prefix + 'Finished');
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all messages!');
imap.end();
});
});
});
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();
Note: username/password is correct and I had enable IMAP/POP in my gmail account but still I'm getting following error:
{ Error: Invalid credentials (Failure)
at Connection._resTagged (xxx/server/node_modules/imap/lib/Connection.js:1502:11)
at Parser.<anonymous> (xxx/server/node_modules/imap/lib/Connection.js:194:10)
at Parser.emit (events.js:193:13)
at Parser._resTagged (xxx/server/node_modules/imap/lib/Parser.js:175:10)
at Parser._parse (xxx/server/node_modules/imap/lib/Parser.js:139:16)
at Parser._tryread (xxx/server/node_modules/imap/lib/Parser.js:82:15)
at TLSSocket.Parser._cbReadable (xxx/server/node_modules/imap/lib/Parser.js:53:12)
at TLSSocket.emit (events.js:193:13)
at emitReadable_ (_stream_readable.js:550:12)
at processTicksAndRejections (internal/process/task_queues.js:81:17)
type: 'no',
textCode: 'AUTHENTICATIONFAILED',
source: 'authentication' }
Connection ended
Upvotes: 4
Views: 4096
Reputation: 21
Set up 2 factor auth and provide app password for your gmail account.
Use that generated app password on your Imap passwords.
Worked for me!!
var imapConfig = {
user: `${your mail}`,
password: `${your app passowrd}`,
host: 'imap.gmail.com',
port: 993,
tls: true,
tlsOptions:{rejectUnauthorized:false}
};
Upvotes: 1
Reputation: 1
For anyone coming to this thread, if the previous recommendations do not help resolve your problem check your script to ensure that you have properly closed your IMAP session with imap.end().
In the above example imap.end() gets triggers if 'f' is enacted upon, reads messages and then 'ends'.
f.once('end', function() {
console.log('Done fetching all messages!');
imap.end();
});
However, if there are no messages, there is no other place in the script to handle closing the IMAP connection (see below):
imap.once('error', function(err) {
console.log(err);
});
imap.once('end', function() {
console.log('Connection ended');
});
imap.connect();
This will leave the session open. Most email systems have a max of 20 IMAP connections allowed. On the 21st try your script will receive a login failure even though the email and password are correct.
The IMAP block is temporary but could last many hours. Wait it out, fix the script and try again.
Upvotes: 0
Reputation: 854
Note: If using a Gmail account, you will need to setup oAuth:
Upvotes: 1