本文章可以帮你了解,如何使用NodeJS发邮件。
介绍 目前微信端主要使用微信授权登录,或者手机号登录,手机端很少有用邮件登录的了,但是网站还是有不少支持邮件登录的,需要给新用户发送激活邮件。 有邮件通知用户的应用场景,就适合继续看这篇文章。
以GMail为例,使用Google账号发送邮件,请执行以下步骤: 1: 进入链接 https://accounts.google.com/DisplayUnlockCaptcha,允许访问; 2: 进入链接 https://myaccount.google.com/lesssecureapps,允许访问; 3: 添加以下代码到你的程序里: ` var fs = require(‘fs’); var path = require(‘path’); var smtpTransport = require(‘nodemailer-smtp-transport’); var config = JSON.parse(fs.readFileSync(“config.json”)); //its optional to keep password safe var nodemailer = require(‘nodemailer’);
let transporter = nodemailer.createTransport({ service: ‘gmail’, host: ‘smtp.gmail.com’, auth:{ user: ‘myEmailId@gmail.com’, //your mail id pass: ‘xxxxxxxxxxx’ //password of your account }, tls: { rejectUnauthorized: false } });
let HelperOptions = { from: ‘“username” myEmailId@gmail.com’, //your mail id to: ‘myFriendEmail@gmail.com’, // to whom you are sending subject: ‘Marriage Invitation’, text: “Hi Friend…” }; transporter.sendMail(HelperOptions, (error, info)=>{ if (error) { return console.log(error); } console.log(“email sent”); console.log(info); }); ` 用163邮箱,需要去163邮箱的设置界面,允许SMTP发送邮件。
还有更简单的,不过容易被拦截 https://www.npmjs.com/package/sendmail-lite