NestJs配置发送邮件

安装插件

pnpm add nodemailer

配置发件邮箱(以163邮箱为例)

  1. 登录进入邮箱后,点击顶部「设置」按钮,选择「常规设置」进入邮箱设置页面
  2. 选择「POP3/SMTP/IMAP」设置
  3. 开启IMAP/SMTP服务POP3/SMTP服务
  4. 授权密码管理选项新增授权密码,设置好授权密码后要好好保存,只有在新增的时候可以查看一次授权密码
  5. 设置完成后就可以开始定义邮件服务了

自定义邮件服务

@@filename(mail.service.ts)
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import * as nodemailer from 'nodemailer'
 
// 自定义邮件服务
@Injectable()
export class MailService {
  private transporter: nodemailer.Transporter
  constructor(
    private readonly configService: ConfigService,
  ) {
    // 创建一个Nodemailer transporter实例
    this.transporter = nodemailer.createTransport({
      // SMTP服务器主机
      host: 'smtp.163.com',
      // SMTP服务器端口
      port: 465,
      // 使用 SSL
      secure: true,
      auth: {
        // 发件邮箱
        user: 'email@163.com',
        // 授权码
        pass: '*********',
      },
    })
  }
 
  async sendVerificationCode(email:string, message: string) {
    const mailOptions: nodemailer.SendMailOptions = {
      // 发件邮箱
      from: 'from@email.com',
      // 收件邮箱
      to: email,
      // 邮件主题
      subject: 'Mail Title',
      // 邮件正文
      html: `<h1>${message}</h1>`,
    }
 
    // 发送邮件
    await this.transporter.sendMail(mailOptions)
  }
}

调用邮件服务(以UserModule为例)

user.module.ts

@@filename(user.module.ts)
import { Module } from '@nestjs/common'
import { UserService } from './user.service'
import { UserController } from './user.controller'
import { MailService } from 'src/common/mail/mail.service'

/**
 * @description 用户模块
 */
@Module({
  controllers: [ UserController ],
  providers: [ UserService, MailService ],
  exports: [ UserService ]
})

export class UserModule {}

user.service.ts

@@filename(user.service.ts)
import { Injectable } from '@nestjs/common'
import { MailService } from 'src/common/mail/mail.service'

/**
 * @description 用户服务
 */
@Injectable()
export class UserService {
  constructor(
    private readonly mailService: MailService,
  ) {}
  
  async create() {
    await this.mailService.sendVerificationCode('sendmail@mail.com', '123456')
    return 'success'
  }
}

个人博客:https://www.linmeimei.top/ 欢迎访问


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
NestJS中使用Nacos进行配置管理的步骤如下: 1. 安装nacos-sdk-nodejs ```shell npm install nacos-sdk-nodejs --save ``` 2. 在app.module.ts中引入ConfigModule ```typescript import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { NacosConfig } from 'nacos'; @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, load: [], expandVariables: true, cache: true, ignoreEnvFile: true, load: [() => ({ nacos: { host: 'localhost', port: 8848, namespace: 'public', dataId: 'nest-config', group: 'DEFAULT_GROUP', }, })], }), ], }) export class AppModule {} ``` 3. 创建一个config.service.ts文件 ```typescript import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { NacosConfigClient } from 'nacos'; @Injectable() export class NacosConfigService { private nacosConfigClient: NacosConfigClient; constructor(private readonly configService: ConfigService) { const nacosConfig = this.configService.get('nacos'); this.nacosConfigClient = new NacosConfigClient({ serverAddr: `${nacosConfig.host}:${nacosConfig.port}`, namespace: nacosConfig.namespace, }); } async get(key: string): Promise<string> { return await this.nacosConfigClient.getConfig(nacosConfig.dataId, nacosConfig.group); } } ``` 4. 在需要使用配置的地方注入NacosConfigService ```typescript import { Injectable } from '@nestjs/common'; import { NacosConfigService } from './config.service'; @Injectable() export class AppService { constructor(private readonly configService: NacosConfigService) {} async getHello(): Promise<string> { const value = await this.configService.get('key'); return `Hello ${value}!`; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林_深时见鹿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值