我如何使用 imap (nodejs) 接收传入邮件 (gmail) 到我的应用程序服务器
How Can i receive incoming mails(gmail) to my application server using imap (nodejs)
我想使用 imap 阅读完整的传入邮件。但是 imap 只通知我收到的新邮件数量
imap.on('mail', (number) => {
console.log('got mails', number);
});
有什么方法可以让我阅读收到的邮件吗?
如有帮助将不胜感激
- 看看Node.js Quickstart,它会很好地指导你。
- 使用Users.messages: list to get mail ids, here is a direct link of Try this API
- 获得 电子邮件 ID 列表 后,您可以使用 Users.messages: get
- 看看
以下是获取邮件收件箱的分步指南:
- Enable IMAP
- Enable less secure apps
- Install imap package 与
npm install imap
- 使用下一个代码创建index.js:
const Imap = require('imap'), inspect = require('util').inspect;
const myMail = "***@gmail.com";
const myPwd = "***";
let getEmailFromInbox = (mailServer) => {
mailServer.openBox('INBOX', true, function (err, box) {
if (err) throw err;
let f = mailServer.seq.fetch('1:*', {
bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
struct: true
});
f.on('message', function (msg, seqno) {
console.log('Message #%d', seqno);
let prefix = '(#' + seqno + ') ';
msg.on('body', function (stream, info) {
let buffer = '';
stream.on('data', function (chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function () {
console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
});
});
});
f.once('error', function (err) {
console.log('Fetch error: ' + err);
});
f.once('end', function () {
console.log('Done fetching all messages!');
});
});
}
let createLabel = (mailServer, labelName) => {
mailServer.addBox(labelName, (err) => {});
console.log('message', 'New Label or Box Created');
}
let getMailboxStatusByName = (mailServer, inboxName) => {
mailServer.status(inboxName, (err, mailbox) => {
console.log('message', mailbox);
});
console.log('message', 'Label or Box Status');
}
let getMailBoxLabels = (mailServer) => {
mailServer.getBoxes((error, mailbox) => {
console.log('message', mailbox);
})
}
let deleteLabel = (mailServer, labelName) => {
mailServer.delBox(labelName, (error) => {})
console.log('message', 'Label or Box removed');
}
let mailServer1 = new Imap({
user: myMail,
password: myPwd,
host: 'imap.gmail.com',
port: 993,
tls: true,
tlsOptions: {
rejectUnauthorized: false
},
authTimeout: 3000
}).once('error', function (err) {
console.log('Source Server Error:- ', err);
});
mailServer1.once('ready', function () {
mailServer1.openBox('INBOX', true, function (err, box) {
if (err) throw err;
console.log('message', 'server1 ready');
});
// mail operation
//getMailBoxLabels(mailServer1);
getEmailFromInbox(mailServer1)
//createLabel(mailServer1, "demo-label1");
//deleteLabel(mailServer1, "demo-label1");
//getMailboxStatusByName(mailServer1, "INBOX");
})
mailServer1.connect();
- 运行
node index.js
并检查您的邮件输出
根据您的需要调整此代码段
我想使用 imap 阅读完整的传入邮件。但是 imap 只通知我收到的新邮件数量
imap.on('mail', (number) => {
console.log('got mails', number);
});
有什么方法可以让我阅读收到的邮件吗?
如有帮助将不胜感激
- 看看Node.js Quickstart,它会很好地指导你。
- 使用Users.messages: list to get mail ids, here is a direct link of Try this API
- 获得 电子邮件 ID 列表 后,您可以使用 Users.messages: get
- 看看
以下是获取邮件收件箱的分步指南:
- Enable IMAP
- Enable less secure apps
- Install imap package 与
npm install imap
- 使用下一个代码创建index.js:
const Imap = require('imap'), inspect = require('util').inspect;
const myMail = "***@gmail.com";
const myPwd = "***";
let getEmailFromInbox = (mailServer) => {
mailServer.openBox('INBOX', true, function (err, box) {
if (err) throw err;
let f = mailServer.seq.fetch('1:*', {
bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
struct: true
});
f.on('message', function (msg, seqno) {
console.log('Message #%d', seqno);
let prefix = '(#' + seqno + ') ';
msg.on('body', function (stream, info) {
let buffer = '';
stream.on('data', function (chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function () {
console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
});
});
});
f.once('error', function (err) {
console.log('Fetch error: ' + err);
});
f.once('end', function () {
console.log('Done fetching all messages!');
});
});
}
let createLabel = (mailServer, labelName) => {
mailServer.addBox(labelName, (err) => {});
console.log('message', 'New Label or Box Created');
}
let getMailboxStatusByName = (mailServer, inboxName) => {
mailServer.status(inboxName, (err, mailbox) => {
console.log('message', mailbox);
});
console.log('message', 'Label or Box Status');
}
let getMailBoxLabels = (mailServer) => {
mailServer.getBoxes((error, mailbox) => {
console.log('message', mailbox);
})
}
let deleteLabel = (mailServer, labelName) => {
mailServer.delBox(labelName, (error) => {})
console.log('message', 'Label or Box removed');
}
let mailServer1 = new Imap({
user: myMail,
password: myPwd,
host: 'imap.gmail.com',
port: 993,
tls: true,
tlsOptions: {
rejectUnauthorized: false
},
authTimeout: 3000
}).once('error', function (err) {
console.log('Source Server Error:- ', err);
});
mailServer1.once('ready', function () {
mailServer1.openBox('INBOX', true, function (err, box) {
if (err) throw err;
console.log('message', 'server1 ready');
});
// mail operation
//getMailBoxLabels(mailServer1);
getEmailFromInbox(mailServer1)
//createLabel(mailServer1, "demo-label1");
//deleteLabel(mailServer1, "demo-label1");
//getMailboxStatusByName(mailServer1, "INBOX");
})
mailServer1.connect();
- 运行
node index.js
并检查您的邮件输出
根据您的需要调整此代码段