尝试使用node-imap删除邮件

时间:2016-09-10 20:00:36

标签: node.js imap

尝试使用 node-imap 模块删除 node.js 中的电子邮件。

我在读/写模式下打开 INBOX

Permission denied (publickey).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.1]

然后获取所有消息:

imap.openBox('INBOX', false, cb);

标记要删除的邮件:

var f = imap.seq.fetch("1:"+box.messages.total, {
      bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)','TEXT'],
      struct: true
    });

关闭 autopurge 设置为true的邮箱

msg.on('end', function() {

    imap.seq.addFlags(seqno, '\\Deleted', function(err) { } );
  });

但这不起作用。我做错了什么?

2 个答案:

答案 0 :(得分:3)

imap.setFlags()适用于UID。 imap.seq.setFlags()适用于序列号。由于您似乎正在尝试传递序列号,因此您应该使用后一种功能。

答案 1 :(得分:0)

const mailBox = new imap({
  host: "imap.gmail.com",
  password: GMAIL_PASSWORD,
  port: 993,
  tls: true,
  user: GMAIL
});

mailBox.once("error", console.error);
mailBox.once("ready", () => {
  mailBox.openBox("INBOX", false, (error, box) => {
    if (error) throw error;

    mailBox.search(
      [
        "UNSEEN",
        ["SUBJECT", "xxx"],
        ["FROM", "no-reply@xxx.xxx"]
      ],
      (error, results) => {
        if (error) throw error;

        for (const uid of results) {
          const mails = mailBox.fetch(uid, {
            bodies: ""
            // markSeen: true
          });
          mails.once("end", () => mailBox.end());

          mails.on("message", (message, seq) => {
            message.on("body", stream => {
              let buffer = "";
              stream.on("data", chunk => (buffer += chunk.toString("utf8")));
              stream.once("end", () => mailBox.addFlags(uid, "Deleted"));
            });
          });
        }
      }
    );
  });
});

mailBox.connect();