摘要:本篇Node.js教程探讨了Node.js发送邮件,希望阅读本篇文章以后大家有所收获,帮助大家对Node.js的理解更加深入。
本篇Node.js教程探讨了Node.js发送邮件,希望阅读本篇文章以后大家有所收获,帮助大家对Node.js的理解更加深入。
这里用的是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: 465, // 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 symbolsvar 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 objecttransporter.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: 465, // 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 - 454 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 - 501 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 - 535 Authentication failed
解决方案:
qq邮箱在测试smtp邮件服务器时,一,在qq邮箱,设置,账户设置中.开启下smtp.二,设置一下独立密码.三,在配置smtp服务器的密码时,注意一定要填你设置的独立密码.不要用邮箱登录密码.否则会提示535 Authentication failed错误.
本文由职坐标整理发布,更多相关知识,请关注职坐标WEB前端Node.js频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号