Mail-Listener2 开源项目教程
1. 项目的目录结构及介绍
Mail-Listener2 是一个用于监听和处理电子邮件的开源项目。以下是其基本的目录结构:
mail-listener2/
├── lib/
│ ├── mail-listener2.js
│ └── ...
├── examples/
│ ├── example.js
│ └── ...
├── README.md
├── package.json
└── ...
- lib/: 包含项目的主要代码文件,其中
mail-listener2.js
是核心文件。 - examples/: 包含使用示例,帮助用户快速上手。
- README.md: 项目的说明文档。
- package.json: 项目的依赖和配置文件。
2. 项目的启动文件介绍
项目的启动文件位于 examples/
目录下,通常是 example.js
。这个文件展示了如何使用 Mail-Listener2 监听电子邮件。
const MailListener = require('../lib/mail-listener2');
const mailListener = new MailListener({
username: "imap-username",
password: "imap-password",
host: "imap-host",
port: 993, // imap port
tls: true,
connTimeout: 10000, // Default by node-imap
authTimeout: 5000, // Default by node-imap
debug: null, // Or your custom function with only one incoming argument. Default: null
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX", // mailbox to monitor
searchFilter: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved
markSeen: true, // all fetched email willbe marked as seen and not fetched next time
fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`
mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib.
attachments: true, // download attachments as they are encountered to the project directory
attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments
});
mailListener.start(); // start listening
mailListener.on("server:connected", function(){
console.log("imapConnected");
});
mailListener.on("mailbox", function(mailbox){
console.log("Total number of mails: ", mailbox.messages.total); // this field in mailbox gives the total number of emails
});
mailListener.on("server:disconnected", function(){
console.log("imapDisconnected");
});
mailListener.on("error", function(err){
console.log(err);
});
mailListener.on("mail", function(mail, seqno, attributes){
// do something with the mail object including attachments
console.log("Email content", mail);
});
3. 项目的配置文件介绍
项目的配置文件主要是 package.json
,它包含了项目的依赖、脚本和其他元数据。
{
"name": "mail-listener2",
"version": "0.3.1",
"description": "Mail listener library for node.js. Get notification when new email arrived.",
"main": "lib/mail-listener2.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/chirag04/mail-listener2.git"
},
"keywords": [
"mail",
"job",
"imap",
"mail listener",
"email",
"email parser"
],
"author": "Chirag Jain <jain_chirag04@yahoo.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/chirag04/mail-listener2/issues"
},
"homepage": "https://github.com/chirag04/mail-listener2",
"dependencies": {
"imap": "~0.8.14",
"mailparser": "