nodejs使用nodemailer發送郵件


nodejs使用nodemailer實現自動發送郵件

首先安裝包:

npm i nodermailer --save-dev

 

代碼如下:

// const path = require('path');
const nodemailer = require('nodemailer');
const os = require('os');
// 發送人信息
const MailInfo = {
    service: 'qq',
    user: '1522025433@qq.com',
    pass: 'xxxx' // 授權碼
};

// 接受人信息
const ToInfo = {
    // 接受者,可以同時發送多個,以逗號隔開  (通知自己)
    user: '2122008217@qq.com,' + MailInfo.user,  // 姚德凱
}

// 本機ip
let LocatIp = '';

/**
 * 獲取本機IP
 * @return {String} 返回本機IP
 */
const getLocalIP = ()=>{
    const ifaces = os.networkInterfaces();
    let locatIp = '';
    for (let dev in ifaces) {
        if (dev === '本地連接' || dev === '以太網') {
            for (let j = 0; j < ifaces[dev].length; j++) {
                if (ifaces[dev][j].family === 'IPv4') {
                    locatIp = ifaces[dev][j].address;
                    break;
                }
            }
        }
    }
    return locatIp;
}
LocatIp = getLocalIP();

console.log(LocatIp)


/*
// qq
const transporter = nodemailer.createTransport({
    service: 'qq',
    auth: {
        // user: '527828938@qq.com',
        user: 'zhudongakaiemail@163.com',
        // pass: 'ugxovfwhvxypxxxx' // 授權碼,通過QQ獲取
        pass: 'xxxxx' // 授權碼,通過QQ獲取
    }
});
*/

/*
// 163
const transporter = nodemailer.createTransport({
    // host: 'smtp.163.com',
    // port: 465,
    // secure: true, // 如果是 true 則port填寫465, 如果 false 則可以填寫其它端口號
    service: '163',
    auth: {
        // user: 'zhudongkaiemail@163.com',
        // pass: 'xxxxxxxxx' // 授權碼,通過163獲取
        user: MailInfo.user,
        pass: MailInfo.pass // 授權碼,通過163獲取
    }
});
*/

// outlook
const transporter = nodemailer.createTransport({
    service: MailInfo.service,
    auth: {
        // user: 'zhudongkaiemail@163.com',
        // pass: 'xxxxxxxxx' // 授權碼,通過163獲取
        user: MailInfo.user,
        pass: MailInfo.pass // 授權碼,通過163獲取
    }
});





const mailOptions = {
    // from: '527828938@qq.com', // 發送者
    // from: 'zhudongkaiemail@163.com', // 發送者
    // to: '1689372692@qq.com', // 接受者,可以同時發送多個,以逗號隔開
    from: MailInfo.user, // 發送者
    to: ToInfo.user, // 接受者,可以同時發送多個,以逗號隔開
    subject: '發包郵件發送', // 標題
    //text: 'Hello world', // 文本
    /*
    html: `<h2>nodemailer基本使用:</h2>
           <h3>
                   <a href="http://blog.csdn.net/zzwwjjdj1/article/details/51878392">
                    http://blog.csdn.net/zzwwjjdj1/article/details/51878392
                </a>
            </h3>`,
    */
    html: `<h2>辛苦發包,謝謝!</h2>
           <a href="http://${LocatIp}:5000/build/build.zip">下載正式包</a>
           <a href="http://${LocatIp}:5000/build.test/build.test.zip">下載測試包</a>`,
/*  // 附件
    attachments:[
        // {
        //     filename : 'build.test',
        //     path: path.resolve( __dirname, '../build.test/build.test.zip')
        // },
        // {
        //     filename : 'build',
        //     path: path.resolve( __dirname, '../build/build.zip')
        // }
        
        
        {
            filename : 'build',
            path: path.resolve( __dirname, '../build/build.zip')
        },
    
    ]
*/
};

transporter.sendMail(mailOptions, function (err, info) {
    if (err) {
        console.log(err);
        return;
    }
    
    console.log('郵件發送成功');
});

這樣就完成了。

如果使用微軟郵箱發送有問題請參考下面這篇文章有可能幫到你:

nodemaierl以hotmail(微軟郵箱)作為發件人時報錯554 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM