• nodejs基于nodemailer插件实现发送邮箱


    官网地址:https://nodemailer.com/about/

    项目中添加插件nodemailer:

    npm install nodemailer
    

    发送邮件例子:

    "use strict";
    const nodemailer = require("nodemailer");
    
    // async..await is not allowed in global scope, must use a wrapper
    async function main() {
      // Generate test SMTP service account from ethereal.email
      // Only needed if you don't have a real mail account for testing
      let testAccount = await nodemailer.createTestAccount();
    
      // create reusable transporter object using the default SMTP transport
      // // 创建发送邮件的对象
      let transporter = nodemailer.createTransport({
        host: "smtp.qq.com",
        port: 456,
        secure: true, // true for 465, false for other ports
        auth: {
          user: '你的邮箱', // generated ethereal user 发送方QQ邮箱
          pass: '你的  mtp 验证码', // generated ethereal password
        },
      });
    
      // send mail with defined transport object
      let info = await transporter.sendMail({
        from: '"Fred Foo " <foo@example.com>', // sender address
        to: "bar@example.com, baz@example.com", // list of receivers
        subject: "Hello ✔", // Subject line
        text: "Hello world?", // plain text body
        html: "<b>Hello world?</b>", // html body
      });
    
      console.log("Message sent: %s", info.messageId);
      // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
    
      // Preview only available when sending through an Ethereal account
      console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
      // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
    }
    
    main().catch(console.error);
    

    注解:

    1. 上面的 mtp 验证码,(以qq邮箱为例)在 “设置”》“账户”》 里面找

    2.其它邮箱 host 在 services.json
    相对路径:\node_modules\nodemailer\lib\well-known\services.json
    截图一些:

    3.报错 “no function … ”
    有没有安装 node.js, nodemail

    4.在运行文件时报Error: self signed certificate in certificate chain 错误

    解决办法,运行时候先把本电脑的防护先关了(防火墙,360,金山毒霸等)

    砥砺前行
  • 相关阅读:
    [LintCode] Cosine Similarity 余弦公式
    Word 2010 给公式添加序号
    Xshell连接不上虚拟机的问题和解决办法
    关于 “VMware Workstation 不可恢复错误- (vcpu-0)”
    TortoiseGit客户端安装及使用(上传代码到git@osc
    Android Studio修改项目名和包名
    Android 环信(Android)设置头像和昵称的方法
    Android SharedPreferences存储map的方法
    Android 环信聊天头像昵称显示解决方案
    Android 判断当前Fragment是否可见(Visible)
  • 原文地址:https://www.cnblogs.com/lhongsen/p/14741357.html
Copyright © 2020-2023  润新知