步骤一,开启邮箱服务
在邮箱设置里面开启POP3/SMTP/IMAP服务,如图
步骤二,安装nodemailer插件
npm install -save nodemailer
步骤三,构造如下代码
const nodemailer=require('nodemailer');
let transporter=nodemailer.createTransport({
host:'smtp.126.com',
secure:true,
auth:{
user:'*****@126.com',
pass:'*****'
},
});
let mailOptions={
from:'******@126.com',
to:'******@163.com',
subject:'天气',
text:'今天天气很好!',
html:'<b>今天天气很好!</b>'
};
transporter.sendMail(mailOptions,(err,info)=>{
if(err){
console.log(err);
}
else{
console.log(info);
}
});
步骤四,测试
注意点
发送程序中,subject,和主题需要相一致,不然导致如下错误,504,SUM 信封发件人和信头发件人不匹配
{ Error: Message failed: 554 DT:SPM 126 smtp7,DsmowAA3nqryKy1Zc0oRJw--.15930S2 1496132596,please see http://mail.163.com/help/help_spam_16.htm?ip=111.74.215.148&hostid=smtp7&time=1496132596
at SMTPConnection._formatError (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:557:19)
at SMTPConnection._actionSMTPStream (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:1385:34)
at SMTPConnection._responseActions.push.str (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:907:22)
at SMTPConnection._processResponse (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:706:20)
at SMTPConnection._onData (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:509:14)
at TLSSocket._socket.on.chunk (D:\node\likeread\node_modules\nodemailer\lib\smtp-connection\index.js:461:47)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
code: 'EMESSAGE',
response: '554 DT:SPM 126 smtp7,DsmowAA3nqryKy1Zc0oRJw--.15930S2 1496132596,please see http://mail.163.com/help/help_spam_16.htm?ip=111.74.215.148&hostid=smtp7&time=1496132596',
responseCode: 554,
command: 'DATA' }
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。