• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LDLX@Y火星
博客园    首页    新随笔    联系   管理    订阅  订阅

nodejs发送邮件

这里我主要使用的是 nodemailer 这个插件

第一步 下载依赖

cnpm install nodemailer --save

第二步 建立email.js

'use strict';

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
  // host: 'smtp.ethereal.email',
  service: 'qq', // 使用了内置传输发送邮件 查看支持列表:https://nodemailer.com/smtp/well-known/
  port: 465, // SMTP 端口
  secureConnection: true, // 使用了 SSL
  auth: {
    user: 'xxxxxx@qq.com',//你的邮箱
    // 这里密码不是qq密码,是你设置的smtp授权码
    pass: 'qq的授权码',
  }
});

let mailOptions = {
  from: '"火星黑洞" <1418275530@qq.com>', // sender address
  to: '156093340@qq.com', // list of receivers
  subject: 'Hello', // Subject line
  // 发送text或者html格式
  // text: 'Hello 我是火星黑洞', // plain text body
  html: '<b>Hello 我是火星黑洞</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    return console.log(error);
  }
  console.log('Message sent: %s', info.messageId);
  // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com>
});

第三步,执行 node email.js,即可发送成功~

 

注:可以前往 https://jingyan.baidu.com/article/fedf0737af2b4035ac8977ea.html 去查看如何设置授权码

 

 
posted @ 2018-10-08 22:54  火星黑洞  阅读(3972)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3