通常我们做node项目时,可能我们会碰到做一个简单的邮件反馈,那么我们今天就来讨论一下,其中遇到的各种坑。

  总的来说做这个东西,我们可能需要node第三方依赖模块,来实现我们要达到的效果。

  这里我推荐两个模块:https://github.com/pingfanren/Nodemailer

npm install nodemailer   //这个模块不错,github上星也比较多,还经常有维护,但是坑也比较多

  另一个,https://github.com/eleith/emailjs

npm install emailjs  --save 

  这里我用的是nodemailer模块,毕竟用的人比较多,跟随主流呢

  它的特点:

  • 使用Unicode编码
  • 支持Windows系统,不需要安装依赖
  • 支持纯文本和HTML格式
  • 支持发送附件(包括大型附件)
  • 在HTML中嵌入图片
  • 支持SSL/STARTTLS安全协议
  • 不同的传输方法,可以使用内置也可以使用外部插件的形式
  • 提供自定义插件支持(比如增加DKIM签名,使用markdown代替HTML等等)
  • 支持XOAUTH2登录验证(以及关于更新的令牌反馈)

安装使用

npm install nodemailer --save

  使用内置传输发送邮件,可以查看支持列表:https://github.com/andris9/nodemailer-wellknown#supported-services

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
//https://github.com/andris9/nodemailer-wellknown#supported-services 支持列表
service: 'qq',
port: , // SMTP 端口
secureConnection: true, // 使用 SSL
auth: {
user: '768065158@qq.com',
//这里密码不是qq密码,是你设置的smtp密码
pass: '*****'
}
}); // NB! No need to recreate the transporter object. You can use
// the same transporter object for all e-mails // setup e-mail data with unicode symbols
var mailOptions = {
from: '768065158@qq.com', // 发件地址
to: '528779822@qq.com', // 收件列表
subject: 'Hello sir', // 标题
//text和html两者只支持一种
text: 'Hello world ?', // 标题
html: '<b>Hello world ?</b>' // html 内容
}; // send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response); });

  发送邮件成功以后我们很少会有操作,但也有极少数情况需要在成功以后会处理一些特殊信息的,这时候info对象就能发挥余热了。info对象中包含了messageId、envelop、accepted和response等属性,具体看文档我不一一介绍了。

  使用其他传输插件   https://github.com/andris9/nodemailer-smtp-transport

npm install nodemailer-smtp-transport  --save

  其他代码类似,差别只是在创建transport上,所以这里我就写一部分代码:

var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport'); // 开启一个 SMTP 连接池
var transport = nodemailer.createTransport(smtpTransport({
host: "smtp.qq.com", // 主机
secure: true, // 使用 SSL
secureConnection: true, // 使用 SSL
port: , // SMTP 端口
auth: {
user: "gaolu19901228@qq.com", // 账号
pass: "******" // 密码
}
})); // 设置邮件内容
var mailOptions = {
from: "768065158<768065158@qq.com>", // 发件地址
to: "528779822@qq.com", // 收件列表
subject: "Hello world", // 标题
text:"hello",
html: "<b>thanks a for visiting!</b> 世界,你好!" // html 内容
} // 发送邮件
transport.sendMail(mailOptions, function(error, response) {
if (error) {
console.error(error);
} else {
console.log(response);
}
transport.close(); // 如果没用,关闭连接池
});

  下面列出了一些发邮件的字段:

  • from 发送者邮箱
  • sender 发送者区域显示的信息
  • to 接收者邮箱
  • cc 抄送者邮箱
  • bcc 密送者邮箱
  • subject 邮箱主题
  • attachments 附件内容
  • watchHtml apple watch指定的html版本
  • text 文本信息
  • html html内容
  • headers 另加头信息
  • encoding 编码格式

  邮件内容使用UTF-8格式,附件使用二进制流。

  附件

  附件对象包含了下面这些属性:

  • filename 附件名
  • content 内容
  • encoding 编码格式
  • path 文件路径
  • contentType 附件内容类型 

常见错误

  1.账号未设置该服务

{ [AuthError: Invalid login -  Authentication failed, please open smtp flag first!]
name: 'AuthError',
data: '454 Authentication failed, please open smtp flag first!',
stage: 'auth' }

  解决方案:
  QQ邮箱 -> 设置 -> 帐户 -> 开启服务:POP3/SMTP服务

  2.发件账号与认证账号不同

{ [SenderError: Mail from command failed -  mail from address must be same as authorization user]
name: 'SenderError',
data: '501 mail from address must be same as authorization user',
stage: 'mail' }

  3.登录认证失败,可能由于smpt独立密码错误导致 我在qq设置的时候就遇到过

Invalid login -  Authentication failed 

  解决方案:

  qq邮箱在测试smtp邮件服务器时,一,在qq邮箱,设置,账户设置中.开启下smtp.二,设置一下独立密码.三,在配置smtp服务器的密码时,注意一定要填你设置的独立密码.不要用邮箱登录密码.否则会提示535 Authentication failed错误.

  

资料参考:

  qq邮箱smpt设置独立密码:http://wap.yzmg.com/app/103426.html

  nodejs使用 nodemailer发送邮件

  Nodejs发邮件组件Nodemailer

  用nodemailer组件发邮件

  nodemailer使用过程中发现的一些问题

  nodeemail wellkown

node.js发送邮件email的更多相关文章

  1. Node.js 发送Email

    章节 Node.js 介绍 Node.js 入门 Node.js 模块 Node.js HTTP模块 Node.js 文件系统模块 Node.js URL模块 Node.js NPM Node.js ...

  2. Node.js发送邮件

    1.使用nodemailer模块 var nodemailer = require("nodemailer"); 2.代码如下 exports.send_email = funct ...

  3. 用Node.js发送邮件

    本文讲的是用Node.js通过一个开启smtp的已有的邮箱账号发送邮件,而不是如何创建一个邮件服务器 开启smtp服务 首先要去要使用的邮箱中设置开启smtp,才能正常发送邮件 这边以163邮箱为例 ...

  4. node.js 发送邮件

    var nodemailer = require('nodemailer'); var smtpTransport = require('nodemailer-smtp-transport'); // ...

  5. Node.js 文件系统模块

    章节 Node.js 介绍 Node.js 入门 Node.js 模块 Node.js HTTP模块 Node.js 文件系统模块 Node.js URL模块 Node.js NPM Node.js ...

  6. Node.js 介绍

    章节 Node.js 介绍 Node.js 入门 Node.js 模块 Node.js HTTP模块 Node.js 文件系统模块 Node.js URL模块 Node.js NPM Node.js ...

  7. Node.js使用Nodemailer发送邮件

    除了Python,在node中收发电子邮件也非常简单,因为强大的社区有各种各样的包可以供我么直接使用.Nodemailer包就可以帮助我们快速实现发送邮件的功能. Nodemailer简介 Nodem ...

  8. node.js 模拟自动发送邮件验证码

    node.js 模拟自动发送邮件验证码 引言 正文 1. QQ邮箱设置 2. 安装nodemailer 3.配置信息 4.综合 5.讲解 结束语 引言 先点赞,再看博客,顺手可以点个关注. 微信公众号 ...

  9. 基于Node.js的强大爬虫 能直接发布抓取的文章哦

    基于Node.js的强大爬虫 能直接发布抓取的文章哦 基于Node.js的强大爬虫能直接发布抓取的文章哦!本爬虫源码基于WTFPL协议,感兴趣的小伙伴们可以参考一下 一.环境配置 1)搞一台服务器,什 ...

随机推荐

  1. Unity打包IOS和Android以及之间的交互

    1.导出的Xcode工程 主要讲解Unity导出的Xcode工程的目录结构 2.导出的Android-Eclipse工程 主要讲解Unity导出的Android-Eclipse工程的目录结构 3.导出 ...

  2. 简要介绍DES、RSA MD5 sha1 四种加密算法的优缺点,以及使用场合

    美国数据加密标准(DES)是对称密码算法,就是加密密钥能够从解密密钥中推算出来,反过来也成立.密钥较短,加密处理简单,加解密速度快,适用于加密大量数据的场合.RSA是非对称算法,加密密钥和解密密钥是不 ...

  3. Yslow-23条军规

    YslowYahoo发布的一款基于FireFox的插件,主要是为了提高网页性能而设计的,下面是它提倡了23条规则,还是很不错的,分享一下: 1.减少HTTP请求次数 合并图片.CSS.JS,改进首次访 ...

  4. [Unity3D] 01 - Try Unity3D

    01. Move and Rotate 标准全局坐标系 Keyboard using UnityEngine; using System.Collections; public class NewBe ...

  5. 【转】WCF OpenTimeout, CloseTimeout, SendTimeout, ReceiveTimeout

    关于这四个属性,在MSDN中的解释有点敷衍了事.Open/Close/Receive/Send本是HTTP/TCP/SOCKET的概念,Read/Write Operation则是Web Servic ...

  6. Tomcat 8

    JDTCompiler.java /** * Compile the jsp file from the current engine context. As an side- effect, * t ...

  7. PHP 使用 MongoDB

    PHP 想要往 MongoDB 里增删查改数据,需要先安装 mongodb 或 mongo 扩展模块,一般两个都装上: cd /usr/local/src/ wget https://pecl.php ...

  8. 使用pyenv管理不同的python版本

    1. pvenv的安装 git clone https://github.com/yyuu/pyenv.git ~/.pyenv echo 'export PYENV_ROOT="$HOME ...

  9. Dictionary的应用

    在C#中,Dictionary提供快速的基于兼职的元素查找.他的结构是这样的:Dictionary<[key], [value]> ,当你有很多元素的时候可以使用它.它包含在System. ...

  10. matplotlib包画基本的图

    画直线图 1.最简单的用法: import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) #在(-1,1)范围内 ...