本文主要是介绍Node中使用QQ邮箱,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
废话不多说,直接开始。
1.安装包
yarn add nodemailer
2.封装类
import * as nodemailer from 'nodemailer';export class Emailer {private transporter: nodemailer.Transporter;constructor() {this.transporter = nodemailer.createTransport({host:'smtp.qq.com',port:465,secure:true,auth:{user:'发送方的qq邮箱',pass:'密钥'}})}/*** @function sendEmail 发送邮件* @param to 接收方邮箱* @param subject 邮件主题* @param html 邮件内容* @returns */async sendEmail(to: string, subject: string, html: string) {const mailOptions = {from: cfg.email.auth.user,to,subject,html,};return this.transporter.sendMail(mailOptions);}/*** @function sendEmailWithAttachment 发送带附件的邮件* @param to 接收方邮箱* @param subject 邮件主题* @param html 邮件内容* @param attachment 附件* @returns */async sendEmailWithAttachment(to: string, subject: string, html: string, attachment: nodemailer.Attachment) {const mailOptions = {from: cfg.email.auth.user, // 发送方地址to, // 接收方地址subject, // 邮件主题html, // 邮件内容attachments: [attachment] // 附件}return this.transporter.sendMail(mailOptions);}
}
然后就能直接使用了。
这篇关于Node中使用QQ邮箱的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!