如何通过node js下载outlook附件
How to download outlook attachment through node js
我正在使用以下代码下载 Gmail 附件。在 windows 中,这工作正常,但是当我试图在我的 Linux 机器中 运行 相同的代码时,它给我一个错误,如下所示。
也有人可以帮助我如何对 Office Outlook 执行相同的操作。因为我收到身份验证错误
错误:
Downloading attachments for pmgtesting1@gmail.com since 2020-11-25 to files/...
[connection] Connected to host
<= '* OK Gimap ready for requests from 51.103.27.188 l25mb237269790wmj'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! l25mb237269790wmj'
=> 'A1 LOGIN "pmgtesting1@gmail.com" "pswd****"'
<= '* NO [WEBALERT https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv-cVj07x6UiZjwizvTzmqYRNNyofoJ8yiGr960BU9t1OKkU83L43n5ocMP2SlhJ3MqWkb04Wj4ye1XxDbTSALJhCRkQ373HLB_tcH_yHBmmDycIiLGnX3NNETY0QakllRJFVjR] Web login required.'
<= 'A1 NO [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)'
[connection] Ended
[connection] Closed
disconnected from pmgtesting1@gmail.com
Error ----> {
latestTime: undefined,
lastSyncId: 0,
uidvalidity: undefined,
handledIds: [],
errors: [
Error: Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)
at Connection._resTagged (/home/textify/Mail-attachement/node_modules/imap/lib/Connection.js:1488:11)
at Parser.<anonymous> (/home/textify/Mail-attachement/node_modules/imap/lib/Connection.js:193:10)
at Parser.emit (events.js:314:20)
at Parser._resTagged (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:175:10)
at Parser._parse (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:139:16)
at Parser._tryread (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:82:15)
at TLSSocket.Parser._cbReadable (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:53:12)
at TLSSocket.emit (events.js:314:20)
at emitReadable_ (_stream_readable.js:558:12)
at processTicksAndRejections (internal/process/task_queues.js:83:21) {
type: 'no',
textCode: 'ALERT',
source: 'authentication'
}
]
}
注意:我启用了安全性较低的应用程序连接
const downloadEmailAttachments = require('download-email-attachments');
const moment = require('moment');
const todaysDate = moment().format('YYYY-MM-DD');
var reTry = 1;
var paraObj = {
invalidChars: /\W/g,
account: `"****@gmail.com":Paswd******@imap.gmail.com:993`,
// all options and params
//besides account are optional
directory: './files',
filenameTemplate: '{filename}',
// filenameTemplate: '{day}-{filename}',
filenameFilter: /.txt?$/,
timeout: 10000,
log: { warn: console.warn, debug: console.info, error: console.error, info:
console.info },
since: todaysDate,
lastSyncIds: ['234', '234', '5345'], // ids already dowloaded and ignored, helpful
//because since is only supporting dates without time
attachmentHandler: function (attachmentData, callback, errorCB) {
console.log(attachmentData);
callback()
}
}
var onEnd = (result) => {
if (result.errors || result.error) {
console.log("Error ----> ", result);
if(reTry < 4 ) {
console.log('retrying....', reTry++)
return downloadEmailAttachments(paraObj, onEnd);
} else console.log('Failed to download attachment')
} else console.log("done ----> ");
}
downloadEmailAttachments(paraObj, onEnd);
const downloadEmailAttachments = require('download-email-attachments');
const moment = require('moment');
const todaysDate = moment().format('YYYY-MM-DD');
var reTry = 1;
var paraObj = {
invalidChars: /\W/g,
account: `"****@gmail.com":Paswd******@imap.gmail.com:993`,
// all options and params
//besides account are optional
directory: './files',
filenameTemplate: '{filename}',
// filenameTemplate: '{day}-{filename}',
filenameFilter: /.txt?$/,
timeout: 10000,
log: { warn: console.warn, debug: console.info, error: console.error, info:
console.info },
since: todaysDate,
lastSyncIds: ['234', '234', '5345'], // ids already dowloaded and ignored, helpful
//because since is only supporting dates without time
attachmentHandler: function (attachmentData, callback, errorCB) {
console.log(attachmentData);
callback()
}
}
var onEnd = (result) => {
if (result.errors || result.error) {
console.log("Error ----> ", result);
if(reTry < 4 ) {
console.log('retrying....', reTry++)
return downloadEmailAttachments(paraObj, onEnd);
} else console.log('Failed to download attachment')
} else console.log("done ----> ");
}
downloadEmailAttachments(paraObj, onEnd);
我得到的答案是在 Gmail 中启用双因素身份验证,然后注册您的应用程序并使用应用程序密码来获取电子邮件。注意:对于 outlook 部分,我没有管理员权限来生成应用程序密码,如果你有那么这也适用于你将在我找到它们后立即更新其他解决方案
我正在使用以下代码下载 Gmail 附件。在 windows 中,这工作正常,但是当我试图在我的 Linux 机器中 运行 相同的代码时,它给我一个错误,如下所示。 也有人可以帮助我如何对 Office Outlook 执行相同的操作。因为我收到身份验证错误
错误:
Downloading attachments for pmgtesting1@gmail.com since 2020-11-25 to files/...
[connection] Connected to host
<= '* OK Gimap ready for requests from 51.103.27.188 l25mb237269790wmj'
=> 'A0 CAPABILITY'
<= '* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH'
<= 'A0 OK Thats all she wrote! l25mb237269790wmj'
=> 'A1 LOGIN "pmgtesting1@gmail.com" "pswd****"'
<= '* NO [WEBALERT https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv-cVj07x6UiZjwizvTzmqYRNNyofoJ8yiGr960BU9t1OKkU83L43n5ocMP2SlhJ3MqWkb04Wj4ye1XxDbTSALJhCRkQ373HLB_tcH_yHBmmDycIiLGnX3NNETY0QakllRJFVjR] Web login required.'
<= 'A1 NO [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)'
[connection] Ended
[connection] Closed
disconnected from pmgtesting1@gmail.com
Error ----> {
latestTime: undefined,
lastSyncId: 0,
uidvalidity: undefined,
handledIds: [],
errors: [
Error: Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)
at Connection._resTagged (/home/textify/Mail-attachement/node_modules/imap/lib/Connection.js:1488:11)
at Parser.<anonymous> (/home/textify/Mail-attachement/node_modules/imap/lib/Connection.js:193:10)
at Parser.emit (events.js:314:20)
at Parser._resTagged (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:175:10)
at Parser._parse (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:139:16)
at Parser._tryread (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:82:15)
at TLSSocket.Parser._cbReadable (/home/textify/Mail-attachement/node_modules/imap/lib/Parser.js:53:12)
at TLSSocket.emit (events.js:314:20)
at emitReadable_ (_stream_readable.js:558:12)
at processTicksAndRejections (internal/process/task_queues.js:83:21) {
type: 'no',
textCode: 'ALERT',
source: 'authentication'
}
]
}
注意:我启用了安全性较低的应用程序连接
const downloadEmailAttachments = require('download-email-attachments');
const moment = require('moment');
const todaysDate = moment().format('YYYY-MM-DD');
var reTry = 1;
var paraObj = {
invalidChars: /\W/g,
account: `"****@gmail.com":Paswd******@imap.gmail.com:993`,
// all options and params
//besides account are optional
directory: './files',
filenameTemplate: '{filename}',
// filenameTemplate: '{day}-{filename}',
filenameFilter: /.txt?$/,
timeout: 10000,
log: { warn: console.warn, debug: console.info, error: console.error, info:
console.info },
since: todaysDate,
lastSyncIds: ['234', '234', '5345'], // ids already dowloaded and ignored, helpful
//because since is only supporting dates without time
attachmentHandler: function (attachmentData, callback, errorCB) {
console.log(attachmentData);
callback()
}
}
var onEnd = (result) => {
if (result.errors || result.error) {
console.log("Error ----> ", result);
if(reTry < 4 ) {
console.log('retrying....', reTry++)
return downloadEmailAttachments(paraObj, onEnd);
} else console.log('Failed to download attachment')
} else console.log("done ----> ");
}
downloadEmailAttachments(paraObj, onEnd);
const downloadEmailAttachments = require('download-email-attachments');
const moment = require('moment');
const todaysDate = moment().format('YYYY-MM-DD');
var reTry = 1;
var paraObj = {
invalidChars: /\W/g,
account: `"****@gmail.com":Paswd******@imap.gmail.com:993`,
// all options and params
//besides account are optional
directory: './files',
filenameTemplate: '{filename}',
// filenameTemplate: '{day}-{filename}',
filenameFilter: /.txt?$/,
timeout: 10000,
log: { warn: console.warn, debug: console.info, error: console.error, info:
console.info },
since: todaysDate,
lastSyncIds: ['234', '234', '5345'], // ids already dowloaded and ignored, helpful
//because since is only supporting dates without time
attachmentHandler: function (attachmentData, callback, errorCB) {
console.log(attachmentData);
callback()
}
}
var onEnd = (result) => {
if (result.errors || result.error) {
console.log("Error ----> ", result);
if(reTry < 4 ) {
console.log('retrying....', reTry++)
return downloadEmailAttachments(paraObj, onEnd);
} else console.log('Failed to download attachment')
} else console.log("done ----> ");
}
downloadEmailAttachments(paraObj, onEnd);
我得到的答案是在 Gmail 中启用双因素身份验证,然后注册您的应用程序并使用应用程序密码来获取电子邮件。注意:对于 outlook 部分,我没有管理员权限来生成应用程序密码,如果你有那么这也适用于你将在我找到它们后立即更新其他解决方案