大家好,欢迎来到IT知识分享网。
步骤一,开启邮箱服务
在邮箱设置里面开启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' }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/21335.html