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事件。
许可证
麻省理工学院