记录使用nodemailer,让邮箱发送邮件,实现邮箱验证

记录使用nodemailer,让邮箱发送邮件,实现邮箱验证。

1.要先去邮箱官网设置开启smtp

网易的设置

在这里插入图片描述

QQ邮箱设置,拉到最下面

在这里插入图片描述

简单的搭了个express。

cnpm install nodemailer --save
var express= require('express');
var app=express();
const nodemailer = require('nodemailer');
//这个是nodemail支持的邮箱类型,后面是端口,目前只试了qq邮箱和网易邮箱,其他没试过
const serviceType = {
    "126":465,
    "163":25,
    "1und1":465,
    "AOL":465,
    "DebugMail":465,
    "DynectEmail":465,
    "FastMail":465,
    "GandiMail":465,
    "Gmail":465,
    "Godaddy":465,
    "GodaddyAsia":465,
    "GodaddyEurope":465,
    "hot.ee":465,
    "Hotmail":465,
    "iCloud":465,
    "mail.ee":465,
    "Mail.ru":465,
    "Maildev":465,
    "Mailgun":465,
    "Mailjet":465,
    "Mailosaur":465,
    "Mandrill":465,
    "Naver":465,
    "OpenMailBox":465,
    "Outlook365":465,
    "Postmark":465,
    "QQ":465,
    "QQex":465,
    "SendCloud":465,
    "SendGrid":465,
    "SendinBlue":465,
    "SendPulse":465,
    "SES":465,
    "SES-US-EAST-1":465,
    "SES-US-WEST-2":465,
    "SES-EU-WEST-1":465,
    "Sparkpost":465,
    "Yahoo":465,
    "Yandex":465,
    "Zoho":465,
    "qiye.aliyun":465,
}
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/postMail",(req,res)=>{
    if(req.body){
        const {emailType,email,message,emailTitle} = req.body;
        if(!emailType || !email || !message || !emailTitle){
            const p = !emailType?'emailType':!email?"email":!message?"message":!emailTitle?"emailTitle":"丢雷楼某";
            res.json({
                code:0,
                msg:"参数错误,缺少必要参数["+p+"]"
            })
            return 
        }
        let transporter = nodemailer.createTransport({
            service:emailType, // 使用了内置传输发送邮件 查看支持列表:https://nodemailer.com/smtp/well-known/
            port: serviceType[emailType], 
            secureConnection: true, 
            auth: {
            user: 'myEmail@qq.com',//这个是发送的邮箱,qq就qq.com,163就163.com
            pass: 'smtp',//这个是授权码,
            }
        });
        
        let mailOptions = {
            from: '"阿伟"<myEmail@qq.com>', 
            to: email, //对方的邮箱
            subject: emailTitle, //标题
            //文本格式
            text: message,//内容
            //也可以是用html的格式
            //html: "<h1>"+message+"</h1>",
            //附件内容,不用可注释掉
            attachments: [{
                filename: '打球.mp4',
                path: './1.mp4'
            },
            {
                filename: '睡觉.mp4',
                path: './2.mp4'
            },
            {
                filename: '漆美芳.jpg',
                path: './q.jpg'
            },
            {
                filename: '我全都要.gif',
                path: './want.gif'
            },
            {
                filename: 'c测试发送附件.text',
                content: '自定义内容'
            }]
        };
        
        transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
                res.json({
                    code:0,
                    msg:"发送失败!",
                    error
                })
	            return console.log(error);
            }
            res.json({
                code:1,
                msg:"发送成功!",
                reponse:info
            })
        });
    }else{
        res.json({
            code:0,
            msg:"滚",
        })
    }
})

var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('项目启动!! http://%s:%s', host, port);
})

接收情况

QQ邮箱
在这里插入图片描述
网易
在这里插入图片描述


版权声明:本文为weixin_39573946原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。