登录
转载自 https://blog.csdn.net/wsj_weixiao/article/details/126158396。
npm install nodemailer
已启用的 API 和服务
重定向URL 中输入 https://developers.google.com/oauthplayground
配置前面复制的客户端ID与客户端密钥,然后选择gmail Api 并点击Authorize APIs 按钮
账号授权时如果出现 这种提示 则是因为 创建的应用未设置测试用户,将当前用户加入测试用户列表中即可(步骤:2.13,2.14,2.15)
因为这只是测试 App 所以会有提示,点击继续即可
到了这一步我们就创建完成 所需要的信息 下一步就进行代码编写
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: "test@gmail.com",
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: "test@gmail.com",
to: "test@qq.com",
subject: "Gmail 测试邮件",
html: "Gmail 测试邮件 内容",
});
res.send("Hello World!");
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
运行 文件 node app.js 并访问 localhost:3333 过一会我们就能收到 Gmail 发送的邮件了
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 || {};