nodejs使用谷歌邮箱发邮件
nodejs使用谷歌邮箱发邮件
第一步安装 nodemailer
npm install nodemailer第二步 创建Google 项目
























第三步 编写邮件代码

第四部 定时刷新Token
大功告成
Last updated
npm install nodemailer
























Last updated
const express = require("express");
const nodemailer = require("nodemailer");
const app = express();
const port = 3333;
app.get("/", (req, res) => {
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
type: "OAuth2",
user: "[email protected]",
clientId:
"344551453406-rqk1a539bljgc9ldt9gv2kfbv3ud7l9u.apps.googleusercontent.com",
clientSecret: "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",
refreshToken:
"1//0412Uzn4225S4CgYIARAAGAQSNwF-L9IrZNMaQswZxgQ_fvrrS70UAH9_e03DP7_c7MJCenpjY4WZPYagEP31LQ-SRUKuhe86zq8",
accessToken:
"ya29.A0AVA9y1sQzoQjsd1wjcHGa5RYik1Bm1yXMWdMx3_Ddwyxwn6sA96sioaCSOVo4UaztU2tTNP4M36okz4JzTyQpR7hz3rpmt3jEGGO_hIwto52m8qn2_sPfXVIVW0ur1otQnMpToD5_T_fkiDGhPdG_OIGNhOyYUNnWUtBVEFTQVRBU0ZRRTY1ZHI4VkpkVlBFZEdBQnpoeVVkbzQta2FJQQ0163",
},
});
transporter.sendMail({
from: "[email protected]",
to: "[email protected]",
subject: "Gmail 测试邮件",
html: "Gmail 测试邮件 内容",
});
res.send("Hello World!");
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});const res = await ctx.curl("https://accounts.google.com/o/oauth2/token", {
method: "POST",
contentType: "json",
dataType: "json",
data: {
client_id: clientId,
client_secret: clientSecret,
refresh_token: refreshToken,
grant_type: "refresh_token",
},
});
const { access_token } = res.data || {};