前段时间有个很普通的项目需要发邮件的功能,而且是刚开始学nodejs,所以只是搜索了下用什么好的库能实现,就找到了nodemailer了。这篇文章主要是记录一下使用的过程和经验。

如何使用

这里就先上配置

nodejs: v6.9.4

npm: 3.10.10

nodemailer: ^4.1.0

const params = {host: 'smtp.163.com', // 设置服务port: 465, // 端口sercure: true, // 是否使用TLS,true,端口为465,否则其他或者568auth: {user: config.user, // 邮箱和密码pass: config.emailPwd}
}// 邮件信息
const mailOptions = {from: config.user, // 发送邮箱to: config.emailTo, // 接受邮箱subject: config.title, // 标题html: config.html // 内容
}// 发送邮件
const transporter = nodemailer.createTransport(params)
transporter.sendMail(mailOptions, (error, info) => {if (error) {return console.log(error);}console.log('Message %s sent: %s', info.messageId, info.response);// success// ...
})

按照这个格式,发送成功是没什么问题的。不过在密码这里要注意,不是邮箱的登录密码。

163和qq获取授权码

163邮箱的话,要开启POP3/SMTP服务,在设置 --> POP3/SMTP/IMAP页面,设置。开通后会有个授权码的,配置里的密码,就是用这个授权码

qq邮箱的话,同样也要开启这个服务,设置 --> 账户 --> POP3服务,点击开启,就会有个授权码,如果忘了记录,在开启服务下面有个“生成授权码”的,可以获取到的。

这样qq和163就能开启了

Gmail

如果是Gmail的话,这个就有点麻烦了,在nodemailer官网也有说到:

Gmail either works well or it does not work at all. It is probably easier to switch to an alternative service instead of fixing issues with Gmail. If Gmail does not work for you then don't use it.

我这里用自己的谷歌邮箱试了下,在上面的配置修改一下

const params = {service: 'Gmail', // 注意,host修改为serviceport: 465, // 端口sercure: true, // 是否使用TLS,true,端口为465,否则其他或者568auth: {user: config.user, // 邮箱和密码pass: config.emailPwd}
}

如果这样不行的话,打开这个链接,开启谷歌允许不够安全应用

如果还不行的话,就要配置谷歌的XOAuth2了

配置XOAuth2

就算使用了这种方法,也未必可能成功

这是我在网上找到的配置XOAuth2的教程

在这里创建一个凭据,选择OAuth客户端ID,应用类型选择“网页应用”,然后填写名称,重定向URI要记得填写,我填写的是这个"https://developers.google.com/oauthplayground"

然后记录下id和密钥

进入此页面,进行配置

填写之前的id和密钥

然后就是step1,选择api

到step2,点击'Exchange authorization code for tokens'按钮,生成token,这样就完成了。

回到nodejs,重写下配置

const params = {service: 'Gmail',auth: {type: 'OAuth2',user: config.user,clientId: config.clientId,clientSecret: config.clientSecret,refreshToken: config.refreshToken,accessToken: config.accessToken}
}

我这里测试是成功的(本地服务器),如果还不成功的话,这个你只能在搜搜有什么解决方法了。

这里是官方的OAuth2配置

nodemailer其他功能

这里列出我自己用过的功能,nodemailer看官方介绍,貌似是很多功能的

发送多个用户:

// 在配置发送信息的时候
const mailOptions = {from: config.user, // 发送邮箱to: config.emailTo + ', ' + config.emailTo2 // 用', '分隔
}

发送html内容

const mailOptions = {....html: config.html // 这里可以是html字符串
}

发送附件

可以同时发送多个

const mailOptions = {...attachments: [{filename: 'test.txt',content: 'hello world!'},{filename: 'test.txt',content: 'hello world!',contentType: 'text/plain'}]
}

html 显示图片

如果直接在图片上写上图片地址,在 qq 和谷歌邮箱都是没法直接显示的。qq 邮箱有一个查看图片的按钮,点击后才能显示。

如果使用附件的形式,是可以直接显示图片的。

// 伪代码
var img = require("fs").readFileSync('./images/1.jpeg')
attachments: [{filename: '1.jpeg',content: img,cid: '00000001'}
]html: `<img src='cid:00000001' />`

官方给出的配置

  • filename - filename to be reported as the name of the attached file. Use of unicode is allowed.
  • content - String, Buffer or a Stream contents for the attachment
  • path - path to the file if you want to stream the file instead of including it (better for larger attachments)
  • href – an URL to the file (data uris are allowed as well)
  • contentType - optional content type for the attachment, if not set will be derived from the filename property
  • contentDisposition - optional content disposition type for the attachment, defaults to ‘attachment’
  • cid - optional content id for using inline images in HTML message source
  • encoding - If set and content is string, then encodes the content to a Buffer using the specified encoding. Example values: ‘base64’, ‘hex’, ‘binary’ etc. Useful if you want to use binary attachments in a JSON formatted email object.
  • headers - custom headers for the attachment node. Same usage as with message headers
  • raw - is an optional special value that overrides entire contents of current mime node including mime headers. Useful if you want to prepare node contents yourself

总结

刚使用nodemailer踩了不少坑,弄了我一天,Gmail不怎么稳定,后面主管就叫换成qq了,据说qq企业邮箱发送邮件的次数比较多。这篇文章就是记录下使用nodemailer的一些经验。

nodemailer的使用相关推荐

  1. Node.js使用NodeMailer发送邮件

    var nodemailer = require('nodemailer') var transporter = nodemailer.createTransport("SMTP" ...

  2. nodemailer实现node发送邮件

    作为一个前端er,利用node独立做一些全栈小项目,是很有效率和必要的. 需要: 做一个活动报名页面,用户填好的表单需要被工作人员收到,一想到把数据存数据库,还需要给工作人员写一个管理页面就觉得很麻烦 ...

  3. express利用nodemailer发送邮件(163邮箱)

    Nodemailer 是一个简单易用的Node.js邮件发送组件 首先安装这个组件 npm install nodemailer --save 安装之后,可以在某个get请求下,发送邮件,具体路由代码 ...

  4. nodemailer 附件_如何使用Nodemailer发送带有附件的电子邮件。 Node.js

    nodemailer 附件 In the previous article, "How to send emails using Nodemailer?", we discusse ...

  5. NodeMailer

    刚开始学习MEAN, 搞个插件发个邮件. NodeMailer貌似出镜率很高,用用. https://nodemailer.com/smtp/ 先申请了个个人的Outlook的邮箱,测试了一把,顺利通 ...

  6. nodemailer使用_如何使用Nodemailer使用HTML作为内容发送电子邮件 Node.js

    nodemailer使用 Prerequisite: 先决条件: How to send emails using Nodemailer | Node.js 如何使用Nodemailer发送电子邮件. ...

  7. Express使用nodemailer完成邮箱验证功能详细流程(含封装,可作自定义模块)

    Express使用nodemailer完成邮箱验证功能详细流程(含封装,可作自定义模块) 记录大创项目中,在express中使用第三方模块nodemailer完成邮箱验证功能,含客户端请求验证邮件和服 ...

  8. nodejs nodemailer

    nodejs & nodemailer https://www.npmjs.com/package/nodemailer 上面的連接裏面 有有一個例子: 可以直接拿來用: 安裝依賴,在pack ...

  9. 用nodejs向163邮箱, gmail邮箱, qq邮箱发邮件, nodemailer使用详解

    准备工作 准备一个163的邮箱, 并获取授权码 在本地机器安装nodejs "注释详尽"的源码 'use strict'; const nodemailer = require(' ...

最新文章

  1. redis python zset
  2. AsyncQueryHandler了解
  3. 040_Tooltip文字提示
  4. ...python の 学习
  5. python将数据集分成训练样本和类标签
  6. 优先级调度算法实现_React17新特性:启发式更新算法
  7. JavaScript汉字Unicode编码相互转换
  8. hcna(华为)_Telnet篇
  9. L2-002 链表去重 (25 分)—团体程序设计天梯赛
  10. Atitit 全文检索 pgsql 艾提拉总结 分词 全文索引的实现要靠 PgSQL 的 gin 索引。分词功能 PgSQL 内置了英文、西班牙文等,但中文分词需要借助开源插件 zhparser;
  11. 056.单链表就地逆置
  12. 西门子1200PLC和Modbus485从站设备通讯
  13. 我的markdown编辑器
  14. java窗口聊天_java聊天窗口的实现
  15. 刘志军的高铁遗产 ——看看日本高铁是怎么建起来的
  16. 图片放大不清晰怎么办?​无损放大可解决
  17. 游戏数值策划属性篇(二):属性价值评估
  18. JAVA租车网站计算机毕业设计Mybatis+系统+数据库+调试部署
  19. VS加载项目后无法打开源文件
  20. 经典排序之二 快速排序 + 二路归并

热门文章

  1. 地宫取宝|2014年蓝桥杯B组题解析第九题-fishers
  2. 最近点对模板__hdu1007
  3. netty权威指南学习笔记三——TCP粘包/拆包之粘包现象
  4. 升级nodejs至最新
  5. 使用html+css+js实现3D相册
  6. Highcharts在IE8中不能一次性正常显示的一种解决办法
  7. Scrapy学习之第一个简单爬取小程序
  8. 【SpringCloud从0到6】 第一节:初识微服务微服务的雪崩效应
  9. html5 标准结构_IT兄弟连 HTML5教程 HTML文件的主体结构
  10. idea报“Usage of API documented as @since 1.7”这一问题的解决方法