如何下载电子邮件附件节点js
我在我的电子邮件收件箱中有一个带有xml附件的电子邮件,我想下载节点js我怎么能这样做是否有任何spesfic模块,我可以使用它我试过mail-listener
mail-listener2
mail-notifier
但没有人适合我。
首先我尝试了邮件监听器,但是我收到了这个错误:
this.imap = new ImapConnection({
^
TypeError: undefined is not a function
但是当我在google上搜索它时我什么都没找到,所以我尝试了mail-listener2,我收到了这个错误:
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
source: 'socket' }
当我谷歌它,我发现一个堆栈链接,说邮件监听器2不起作用,建议使用mail-notifier
node.js - mail-listener2 doesn't work
最后我尝试了mail-notifier
,我又得到了一个错误
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
我厌倦了这里有什么问题?
回答如下:试试这段代码
通过激活适合我的2way身份验证,确保您为您的Gmail帐户使用应用密码。
这将下载邮件附件。
确保在运行之前安装所有依赖项
var fs = require("fs");
var buffer = require("buffer");
var Imap = require("imap");
const base64 = require('base64-stream')
var imap = new Imap({
user: "[email protected]",
password: "xxxxxxxxxxxxxx",
host: "imap.gmail",
port: 993,
tls: true
//,debug: function(msg){console.log('imap:', msg);}
});
function toUpper(thing) {
return thing && thing.toUpperCase ? thing.toUpperCase() : thing;
}
function findAttachmentParts(struct, attachments) {
attachments = attachments || [];
for (var i = 0, len = struct.length, r; i < len; ++i) {
if (Array.isArray(struct[i])) {
findAttachmentParts(struct[i], attachments);
} else {
if (
struct[i].disposition &&
["INLINE", "ATTACHMENT"].indexOf(toUpper(struct[i].disposition.type)) >
-1
) {
attachments.push(struct[i]);
}
}
}
return attachments;
}
function buildAttMessageFunction(attachment) {
var filename = attachment.params.name;
var encoding = attachment.encoding;
console.log(attachment);
return function(msg, seqno) {
var prefix = "(#" + seqno + ") ";
msg.on("body", function(stream, info) {
console.log(info);
//Create a write stream so that we can stream the attachment to file;
console.log(prefix + "Streaming this attachment to file", filename, info);
var writeStream = fs.createWriteStream('2'+filename);
writeStream.on("finish", function() {
console.log(prefix + "Done writing to file %s", filename);
});
// stream.pipe(writeStream); this would write base64 data to the file.
// so we decode during streaming using
if (toUpper(encoding) === "BASE64") {
console.log(writeStream);
if (encoding === 'BASE64') stream.pipe(new base64.Base64Decode()).pipe(writeStream)
}
//the stream is base64 encoded, so here the stream is decode on the fly and piped to the write stream (file)
// //var buf = Buffer.from(b64string, 'base64');
// stream.pipe(writeStream);
// // var write64 = Buffer.from(writeStream, "base64");
// // stream.pipe(write64);
// } else {
// //here we have none or some other decoding streamed directly to the file which renders it useless probably
// stream.pipe(writeStream);
// }
});
msg.once("end", function() {
console.log(prefix + "Finished attachment %s", filename);
});
};
}
imap.once("ready", function() {
imap.openBox("INBOX", true, function(err, box) {
if (err) throw err;
var f = imap.seq.fetch("1:10", {
bodies: ["HEADER.FIELDS (FROM TO SUBJECT DATE)"],
struct: true
});
f.on("message", function(msg, seqno) {
console.log("Message #%d", seqno);
var prefix = "(#" + seqno + ") ";
msg.on("body", function(stream, info) {
var buffer = "";
stream.on("data", function(chunk) {
buffer += chunk.toString("utf8");
});
stream.once("end", function() {
console.log(prefix + "Parsed header: %s", Imap.parseHeader(buffer));
});
});
msg.once("attributes", function(attrs) {
var attachments = findAttachmentParts(attrs.struct);
console.log(prefix + "Has attachments: %d", attachments.length);
for (var i = 0, len = attachments.length; i < len; ++i) {
var attachment = attachments[i];
/*This is how each attachment looks like {
partID: '2',
type: 'application',
subtype: 'octet-stream',
params: { name: 'file-name.ext' },
id: null,
description: null,
encoding: 'BASE64',
size: 44952,
md5: null,
disposition: { type: 'ATTACHMENT', params: { filename: 'file-name.ext' } },
language: null
}
*/
console.log(
prefix + "Fetching attachment %s",
attachment.params.name
);
var f = imap.fetch(attrs.uid, {
//do not use imap.seq.fetch here
bodies: [attachment.partID],
struct: true
});
//build function to process attachment message
f.on("message", buildAttMessageFunction(attachment));
}
});
msg.once("end", function() {
console.log(prefix + "Finished email");
});
});
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();
如何下载电子邮件附件节点js
我在我的电子邮件收件箱中有一个带有xml附件的电子邮件,我想下载节点js我怎么能这样做是否有任何spesfic模块,我可以使用它我试过mail-listener
mail-listener2
mail-notifier
但没有人适合我。
首先我尝试了邮件监听器,但是我收到了这个错误:
this.imap = new ImapConnection({
^
TypeError: undefined is not a function
但是当我在google上搜索它时我什么都没找到,所以我尝试了mail-listener2,我收到了这个错误:
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
source: 'socket' }
当我谷歌它,我发现一个堆栈链接,说邮件监听器2不起作用,建议使用mail-notifier
node.js - mail-listener2 doesn't work
最后我尝试了mail-notifier
,我又得到了一个错误
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
我厌倦了这里有什么问题?
回答如下:试试这段代码
通过激活适合我的2way身份验证,确保您为您的Gmail帐户使用应用密码。
这将下载邮件附件。
确保在运行之前安装所有依赖项
var fs = require("fs");
var buffer = require("buffer");
var Imap = require("imap");
const base64 = require('base64-stream')
var imap = new Imap({
user: "[email protected]",
password: "xxxxxxxxxxxxxx",
host: "imap.gmail",
port: 993,
tls: true
//,debug: function(msg){console.log('imap:', msg);}
});
function toUpper(thing) {
return thing && thing.toUpperCase ? thing.toUpperCase() : thing;
}
function findAttachmentParts(struct, attachments) {
attachments = attachments || [];
for (var i = 0, len = struct.length, r; i < len; ++i) {
if (Array.isArray(struct[i])) {
findAttachmentParts(struct[i], attachments);
} else {
if (
struct[i].disposition &&
["INLINE", "ATTACHMENT"].indexOf(toUpper(struct[i].disposition.type)) >
-1
) {
attachments.push(struct[i]);
}
}
}
return attachments;
}
function buildAttMessageFunction(attachment) {
var filename = attachment.params.name;
var encoding = attachment.encoding;
console.log(attachment);
return function(msg, seqno) {
var prefix = "(#" + seqno + ") ";
msg.on("body", function(stream, info) {
console.log(info);
//Create a write stream so that we can stream the attachment to file;
console.log(prefix + "Streaming this attachment to file", filename, info);
var writeStream = fs.createWriteStream('2'+filename);
writeStream.on("finish", function() {
console.log(prefix + "Done writing to file %s", filename);
});
// stream.pipe(writeStream); this would write base64 data to the file.
// so we decode during streaming using
if (toUpper(encoding) === "BASE64") {
console.log(writeStream);
if (encoding === 'BASE64') stream.pipe(new base64.Base64Decode()).pipe(writeStream)
}
//the stream is base64 encoded, so here the stream is decode on the fly and piped to the write stream (file)
// //var buf = Buffer.from(b64string, 'base64');
// stream.pipe(writeStream);
// // var write64 = Buffer.from(writeStream, "base64");
// // stream.pipe(write64);
// } else {
// //here we have none or some other decoding streamed directly to the file which renders it useless probably
// stream.pipe(writeStream);
// }
});
msg.once("end", function() {
console.log(prefix + "Finished attachment %s", filename);
});
};
}
imap.once("ready", function() {
imap.openBox("INBOX", true, function(err, box) {
if (err) throw err;
var f = imap.seq.fetch("1:10", {
bodies: ["HEADER.FIELDS (FROM TO SUBJECT DATE)"],
struct: true
});
f.on("message", function(msg, seqno) {
console.log("Message #%d", seqno);
var prefix = "(#" + seqno + ") ";
msg.on("body", function(stream, info) {
var buffer = "";
stream.on("data", function(chunk) {
buffer += chunk.toString("utf8");
});
stream.once("end", function() {
console.log(prefix + "Parsed header: %s", Imap.parseHeader(buffer));
});
});
msg.once("attributes", function(attrs) {
var attachments = findAttachmentParts(attrs.struct);
console.log(prefix + "Has attachments: %d", attachments.length);
for (var i = 0, len = attachments.length; i < len; ++i) {
var attachment = attachments[i];
/*This is how each attachment looks like {
partID: '2',
type: 'application',
subtype: 'octet-stream',
params: { name: 'file-name.ext' },
id: null,
description: null,
encoding: 'BASE64',
size: 44952,
md5: null,
disposition: { type: 'ATTACHMENT', params: { filename: 'file-name.ext' } },
language: null
}
*/
console.log(
prefix + "Fetching attachment %s",
attachment.params.name
);
var f = imap.fetch(attrs.uid, {
//do not use imap.seq.fetch here
bodies: [attachment.partID],
struct: true
});
//build function to process attachment message
f.on("message", buildAttMessageFunction(attachment));
}
});
msg.once("end", function() {
console.log(prefix + "Finished email");
});
});
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();