node.js - 如何下载电子邮件附件 Node js

标签 node.js email

我的电子邮件收件箱中有一封带有 xml 附件的电子邮件,我想下载 Node js,我该怎么做?是否有任何可以使用它的特定模块我尝试了 mail-listener mail-listener2 mail-notifier 但没有人适合我。
首先我尝试了邮件监听器 但我收到了这个错误:

      this.imap = new ImapConnection({
                  ^
TypeError: undefined is not a function  

但是当我在谷歌上搜索时,我没有发现任何东西 我尝试了 mail-listener2 并收到此错误:

{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  source: 'socket' }

当我用谷歌搜索时,我发现一个堆栈链接说 mail-listener2 不起作用并建议使用 mail-notifier
node.js - mail-listener2 doesn't work
最后我尝试了 mail-notifier 并收到了另一个错误

Error: connect ECONNREFUSED
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)

我厌倦了这里出了什么问题?

最佳答案

This code will automatically download email attachments since particular date

(这里我采用了今天的日期)。

您可以通过
中的processMails功能修改日期条件 node_modules\download-email-attachments\lib\find-emails.js

这可能有帮助https://gist.github.com/martinrusev/6121028

1) 首先通过gmail 设置为您的gmail 启用imap。 --> https://support.cloudhq.net/how-to-check-if-imap-is-enabled-in-gmail-or-google-apps-account/

2) 如果文件的输出名称不太好,只需转到 save-attachment-stream.js 并删除 var generatedFileName .replace 方法。

node_modules\download-email-attachments\lib\save-attachment-stream.js

var generatedFileName = replaceTemplate(state.filenameTemplate,meta)//.replace(state.invalidChars, '_')

app.js

const downloadEmailAttachments = require('download-email-attachments');
const moment = require('moment');
const opDir = "C:/Users/admin/Desktop/Attachments";
const email = "****@gmail.com";
const password =  "*********";
const port = 993;
const host = 'imap.gmail.com';
const todaysDate = moment().format('YYYY-MM-DD');
var reTry = 1;

var paraObj = {
   invalidChars: /\W/g,
   account: `"${email}":${password}@${host}:${port}`, // all options and params 
   //besides account are optional
   directory: opDir,
   filenameTemplate: '{filename}',
   // filenameTemplate: '{day}-{filename}',
   filenameFilter: /.csv?$/,
   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);

关于node.js - 如何下载电子邮件附件 Node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26980816/

相关文章:

node.js - 在 node.js 中正确使用 _writev

amazon-web-services - 接收/阅读来自 SES 的电子邮件

HTML 电子邮件模板 Mailchimp、Outlook 和 Gmail 中的 CSS 问题

ruby-on-rails - 如何使用 Mandrill 发送和接收邮件?

node.js - Gulp 可以用于将图像转换为下一代格式吗?

node.js - 如何在 Visual Studio 中运行 Cordova/Ionic 命令?

spring-boot - 通过 somain 注册商 1and1 smtp 使用 Spring 邮件发送邮件时遇到问题

java - 在 Apache Commons Mail 中设置退回地址

angularjs - Sails.js + Passport.js 获取当前登录用户

node.js - Docker 不公开 Node 和 webpack 开发服务器的端口