nestjs-imap

中文文档
1.0.7发布于 2024/08/02MITTypeScriptCommonJS

一个使用node-imap包与IMAP服务器交互的nesstjs模块。

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

中文使用文档
文档目录8 节
  1. 安装
  2. 配置
  3. 示例配置
  4. 用法
  5. 示例服务
  6. API
  7. ImapService
  8. 许可证

nestjs-imap

一个用于与IMAP服务器交互的nesjs模块 node-imap 包中。

安装

要安装该包,请运行:

npm install nestjs-imap

配置

要使用 nestjs-imap 模块,您需要将其导入到您的NestJS模块中,并提供必要的IMAP配置。

示例配置

import { Module } from '@nestjs/common';
import { ImapModule, ImapConfig } from 'nestjs-imap';

const imapConfig: ImapConfig = {
  user: '[email protected]',
  password: 'your-password',
  host: 'imap.example.com',
  port: 993,
  tls: true,
};

@Module({
  imports: [ImapModule.forRoot(imapConfig)],
})
export class AppModule {}

用法

你可以使用 ImapService 与IMAP服务器交互。下面是一个如何使用该服务的示例:

示例服务

import { Injectable, OnModuleInit } from '@nestjs/common';
import { ImapService } from 'nestjs-imap';

@Injectable()
export class EmailService implements OnModuleInit {
  constructor(private readonly imapService: ImapService) {}

  async onModuleInit() {
    this.imapService.connect();
    this.imapService.on('ready', async () => {
      // Open the mailbox in read-write mode to allow modifying message flags
      await this.imapService.openBox('INBOX', false);
      const messages = await this.imapService.getMessages(['UNSEEN'], {
        bodies: '',
      });

      console.log('Messages:', messages);

      if (messages.length > 0) {
        const message = messages[0];
        const uid = message.attributes.uid;

        console.log('UID:', uid);

        if (uid) {
          try {
            const fullMessage = await this.imapService.readMessage(uid);
            console.log('Full Message:', fullMessage);

            // Mark the message as read
            await this.imapService.markAsRead(uid);
            console.log('Message marked as read');
          } catch (error) {
            console.error('Error:', error);
          }
        } else {
          console.error('Message UID is not valid');
        }
      } else {
        console.log('No new messages');
      }
    });
  }
}

API

ImapService

方法

  • connect ():连接IMAP服务器。
  • 结束():结束IMAP连接。
  • openBox(boxName: string, readOnly: boolean): Promise:打开邮箱。
  • getMessages(criteria: any, fetchOptions: any): Promise<any[]>:获取符合条件的消息。
  • readMessage(uid: number):承诺:按UID读取指定消息。
  • markAsRead(uid: number): Promise:根据UID将特定消息标记为已读。
  • On (event: string, callback:(…)Args: any[]) => void): void:监听IMAP事件。

许可证

麻省理工学院

版本与发布记录

nestjs-imap 共发布 8 个版本,首次发布于 2024/08/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.7latest2024/08/022 年前
1.0.62024/08/022 年前
1.0.52024/08/022 年前
1.0.42024/08/022 年前
1.0.32024/08/022 年前
1.0.22024/08/022 年前

关于 nestjs-imap 的常见问题

nestjs-imap 是什么?

一个使用node-imap包与IMAP服务器交互的nesstjs模块。注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 nestjs-imap?

在项目目录运行 npm install nestjs-imap,或使用 pnpm add / yarn add / bun add。

nestjs-imap 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

nestjs-imap 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

nestjs-imap 可以商用吗?

nestjs-imap 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

nestjs-imap 还在维护吗?

最新版本 1.0.7 发布于 2024/08/02(2 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/08/02。作者已将其标记为弃用,新项目建议寻找替代方案。