node.js - 如何在 Node js 中使用 npm-imap 包读取和解析电子邮件

标签 node.js npm gmail-imap email-parsing

我浏览了许多链接,但在任何地方都找不到完整的解决方案来实现这一点。

最佳答案

var Imap = require('imap'),
  inspect = require('util').inspect;
var fs = require('fs'), fileStream;
var buffer = '';

var myMap;


var imap = new Imap({
  user: "your-mail-id",
  password: "your-mail-password",
  host: "imap.gmail.com", //this may differ if you are using some other mail services like yahoo
  port: 993,
  tls: true,
  connTimeout: 10000, // Default by node-imap 
  authTimeout: 5000, // Default by node-imap, 
  debug: console.log, // Or your custom function with only one incoming argument. Default: null 
  tlsOptions: { rejectUnauthorized: false },
  mailbox: "INBOX", // mailbox to monitor 
  searchFilter: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved 
  markSeen: true, // all fetched email willbe marked as seen and not fetched next time 
  fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`, 
  mailParserOptions: { streamAttachments: true }, // options to be passed to mailParser lib. 
  attachments: true, // download attachments as they are encountered to the project directory 
  attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments 
});

function openInbox(cb) {
  imap.openBox('INBOX', false, cb);
}

imap.once('ready', function () {
  openInbox(function (err, box) {
    if (err) throw err;
    imap.search(['UNSEEN', ['SUBJECT', 'Give Subject Here']], function (err, results) {
      if (err) throw err;
      var f = imap.fetch(results, { bodies: '1', markSeen: true });
      f.on('message', function (msg, seqno) {
        console.log('Message #%d' + seqno);
        console.log('Message type' + msg.text)
        var prefix = '(#' + seqno + ') ';
        msg.on('body', function (stream, info) {
          stream.on('data', function (chunk) {
            buffer += chunk.toString('utf8');
            console.log("BUFFER" + buffer)

          })
          stream.once('end', function () {
            if (info.which === '1') {
              console.log("BUFFER" + buffer)
            }


          });
          console.log(prefix + 'Body');
          stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt'));
        });
        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(); 

请在:Node-Imap 找到有关此的详细说明

关于node.js - 如何在 Node js 中使用 npm-imap 包读取和解析电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48941453/

相关文章:

javascript - 如何获取字符串路径名?

javascript - 应用程序初始化时将环境值存储在 Vue 中

javascript - Node.js/Socket.io 在除 Firefox 之外的所有平台上都可以工作,这可能是缓存问题吗?

node.js - 我怎样才能获得超过最近 15 条推文的信息?

node.js - npm 构建给出 "Ineffective mark-compacts near heap limit Allocation failed"

C# ImapX 2 电子邮件检查代码似乎很慢

jakarta-mail - 我正在使用javamail IMAP,但我不知道如何获取已发送邮件或除收件箱以外的其他文件夹

html - 如果在 Angular 中将 sourcemap 设置为 false 会发生什么

npm - 将 select2 4.0 版与 NPM 和 browserify 结合使用

php - GMAIL imap_open 远程规范无效 (errflg=2)