【问题标题】:download attachment with mail listener使用邮件监听器下载附件
【发布时间】:2018-11-26 11:26:51
【问题描述】:

我正在使用 mail-listener4 来阅读邮件和下载附件。 邮件提取工作完美,问题是当我尝试下载附件时,它被下载为空文件(0 字节)。

这是附件下载的代码。 我正在尝试下载第一个附件作为初步概念证明。

mailListener.on("mail", function(mail, seqno, attributes){
// affichage des informations de mail
console.log("emailParsed", mail.attachments);
attachment=mail.attachments[0];
// console.log(attachment);
var file = fs.createWriteStream("./attachements/"+mailattachments[0].fileName);
  console.log(cpt)
  file.on('pipe',(file)=>{
    console.log("Test Download")
  })
  attachment.stream.pipe(file)
  });

当我尝试这种方式时,它工作得很好:

mailListener.on("attachment", function(attachment){
  console.log("Attachement")
  console.log(attachment.fileName)
  var file = fs.createWriteStream("./attachements/"+attachment.fileName);
  file.on('pipe',(file)=>{
      console.log('Test download ') 
      }); 
 attachment.stream.pipe(file)
  });`

我不明白为什么一个工作,另一个不工作。你能解释一下发生了什么吗?

【问题讨论】:

  • 您是否有理由不简单地采用第二种方法?
  • 我对 JavaScript 电子邮件不熟悉,但如果第一部分 为空,那就不足为奇了。
  • yes @tripleee i wnat 使用 Subject 和 Sender 过滤邮件,第二种方法我没有关于发件人或主题的任何信息
  • 如果您选择[1][2] 而不是[0],会发生什么?
  • :/ 导致异常,无法读取 undefined 的属性,[1] 和 [2] 不存在此数组包含一个元素

标签: node.js email imap


【解决方案1】:

附件文件在触发“附件”事件时下载。

设置附件:true默认会将附件作为缓冲区对象下载到项目目录中。

https://www.npmjs.com/package/mail-listener4

【讨论】:

  • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
  • 我知道文档是这么说的,但它对我不起作用,即使我将附件设置为 true,每个附件都会触发事件附件,但不包含所需的信息(发件人,Subject) 一起工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
  • 2012-04-01
  • 2012-11-11
  • 1970-01-01
  • 2017-09-21
  • 1970-01-01
相关资源
最近更新 更多