【问题标题】:Trying to delete mail using node-imap尝试使用 node-imap 删除邮件
【发布时间】:2017-01-18 16:44:32
【问题描述】:

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

我以读/写模式打开INBOX

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

然后我获取所有消息:

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

标记要删除的邮件:

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

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

关闭邮箱并将autopurge设置为true

imap.closeBox(true);

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

【问题讨论】:

    标签: node.js imap


    【解决方案1】:

    imap.setFlags() 与 UID 一起使用。 imap.seq.setFlags() 使用序列号。由于您似乎在尝试传递序列号,因此您应该改用后一个函数。

    【讨论】:

    • 感谢您的建议,但仍然无法正常工作。我按问题更新了正确的功能,感谢您指出这一点。
    【解决方案2】:
    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();
    

    【讨论】:

      猜你喜欢
      • 2016-03-28
      • 1970-01-01
      • 2019-05-26
      • 2015-07-17
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      相关资源
      最近更新 更多