node-imap
node-imap copied to clipboard
Unable to connect with Office365 Server, where as connection with Gmail server was working fine ??
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.
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
.
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.
You can try setting debug: console.log
to see exactly what is or isn't happening.
@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
@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
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();
@mscdex In my case I just forgot to put the tls
param at true ...
@manoj529 did you find a solution ??
Office365 requires TLS = true. I think this issue can be closed
Error: LOGIN failed.
at Connection._resTagged (C:\Users\VMA\Desktop\vsync_sales_back\node_modules\imap\lib\Connection.js:1502:11)
at Parser.
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