安裝依賴nodemailer:npm install nodemailer -s
配置發件終端:
let senderMsg = nodemailer.createTransport({
host: 'smtp.qq.com', // qq郵箱的服務器
port: 465, // qq郵箱對應的端口
secureConnection: true, // 不寫這句會報錯:Greeting never received
auth: {
user: '[email protected]', // 發件人的郵箱賬號
pass: '*************' // 郵箱的授權碼
}
})
填寫發件信息:
let receiverMsg = {
from: '[email protected]', // 發件人郵箱
to: '[email protected]', // 收件人郵箱
subject: '測試發郵件', // 主題
text: '你能不能收到我發的郵件呢', // 普通文本格式的郵件內容
html: '', // html格式的郵件內容
attachments: [] // 附件
}
發送行爲:
let postMail = function () {
senderMsg.sendMail(receiverMsg,(error,info)=>{
if(error) {
console.log(error);
} else {
console.log(`Message: ${info.messageId}`);
console.log(`sent: ${info.response}`);
}
});
}
QQ郵箱獲取授權碼方法: