尝试使用 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) { } );
});
但这不起作用。我做错了什么?
答案 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();