如何使用 Node.js 邮箱服务进行自动化发送邮件 All In One
如何使用 Node.js 邮箱服务进行自动化发送邮件 All In One
$ npm i -S nodemailer
"use strict";
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
host: "smtp.forwardemail.net",
port: 465,
secure: true,
auth: {
// TODO: replace `user` and `pass` values from <https://forwardemail.net>
user: "REPLACE-WITH-YOUR-ALIAS@YOURDOMAIN.COM",
pass: "REPLACE-WITH-YOUR-GENERATED-PASSWORD",
},
});
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// send mail with defined transport object
const info = await transporter.sendMail({
from: '"Fred Foo 👻" <foo@example.com>', // sender address
to: "bar@example.com, baz@example.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
//
// NOTE: You can go to https://forwardemail.net/my-account/emails to see your email delivery status and preview
// Or you can use the "preview-email" npm package to preview emails locally in browsers and iOS Simulator
// <https://github.com/forwardemail/preview-email>
//
}
main().catch(console.error);
demos
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
// logger: true,
// debug: true,
// 接收 imap / pop
// host: "pop.163.com",
// host: "imap.163.com",
// 发送 smtp ✅
host: "smtp.163.com",
// port: 25,
// ❌ Error: 4056C15DF87F0000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:355:
// port: 465,
port: 994, // SSL
secure: true,
auth: {
user: "admin@163.com",
pass: "******",
},
})
async function test() {
const info = await transporter.sendMail({
from: '"admin" <admin@163.com>',
to: '"user" <xgqfrms@xgqfrms.xyz>',
subject: "邮件主题",
text: "邮件, 纯文本 ✅",
// html: "<b>邮件, HTML 富文本 🚀</b>",
})
console.log(`info`, info)
console.log("Message sent: %s", info.messageId);
}
test().catch(console.error);
https://stackoverflow.com/questions/77004007/nodemailer-default-options-issue/77004119#77004119
defaults options & to
array
let transporter = nodemailer.createTransport(options[, defaults])
https://nodemailer.com/smtp/#authentication
批量发送
email
![image](https://img2023.cnblogs.com/blog/740516/202308/740516-20230831015534775-514899514.png)
![image](https://img2023.cnblogs.com/blog/740516/202308/740516-20230831015812436-1312198054.png)
let transporter = nodemailer.createTransport(options[, defaults])
批量发送
email
![image](https://img2023.cnblogs.com/blog/740516/202308/740516-20230831015534775-514899514.png)
![image](https://img2023.cnblogs.com/blog/740516/202308/740516-20230831015812436-1312198054.png)
const nodemailer = require('nodemailer');
const date = new Date(`2023-01-01`);
console.log(`date =`, date)
// Sun Jan 01 2023 08:00:00 GMT+0800 (China Standard Time)
const transporter = nodemailer.createTransport({
// logger: true,
// debug: true,
// 接收 imap / pop
// host: "pop.163.com",
// host: "imap.163.com",
// 发送 smtp ✅
host: "smtp.163.com",
port: 25,
// ❌ Error: 4056C15DF87F0000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:355:
// port: 465,
port: 994, // SSL
secure: true,
auth: {
// https://forwardemail.net
user: "test@163.com",
pass: "*******",
},
// host: "smtp.gmail.com",
// port: 465,
// secure: true,
// auth: {
// type: "OAuth2",
// user: "user@example.com",
// accessToken: "ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x",
// },
}, {
// defaults object ✅
// from: 'test@163.com',
from: 'admin <test@163.com>',
// ❌❌ callback = Error: Mail command failed: 553 Mail from must equal authorized user
// 覆盖
date: date,
encoding: 'UTF-8',
html: "<b>邮件, HTML 富文本 🚀</b>",
messageId: '1234567890'
})
/*
https://nodemailer.com/smtp/#authentication
defaults – is an object that is going to be merged into every `message` object. This allows you to specify shared options, for example to set the same from address for every message
TS .d.ts
/Users/xgqfrms-mm/Library/Caches/typescript/5.3/node_modules/@types/nodemailer/lib/mailer/index.d.ts
interface Options {
// The e-mail address of the sender. All e-mail addresses can be plain 'sender@server.com' or formatted 'Sender Name <sender@server.com>'
from?: string | Address | undefined;
// An e-mail address that will appear on the Sender: field
sender?: string | Address | undefined;
// Comma separated list or an array of recipients e-mail addresses that will appear on the To: field
to?: string | Address | Array<string | Address> | undefined;
}
*/
async function test() {
const info = await transporter.sendMail({
// from: '"admin" <test@163.com>',
// message 对象,自动合并 default 到这里 ✅
// to: 'user <test@xgqfrms.xyz>',
to: [
'user <test1@xgqfrms.xyz>',
'user <test2@xgqfrms.xyz>',
],
subject: "邮件主题 🇨🇳",
text: "邮件, 纯文本 ❌✅",
// html: "<b>邮件, HTML 富文本 🚀</b>",
}, (err, info) => {
if(err) {
console.log(`❌❌ callback =`, err)
} else {
console.log(`✅✅ callback =`, info)
}
})
console.log(`info`, info)
// console.log("Message sent: %s", info.messageId);
}
test().catch(console.error);
/*
API
https://nodemailer.com/usage/
https://nodemailer.com/smtp/
https://stackoverflow.com/questions/77004007/nodemailer-default-options-issue/77004119?noredirect=1#comment135757691_77004119
*/
// module.exports = {
// monthlyStorageMetrics,
// }
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
SMTP
cURL
post
$ curl -XPOST "http://127.0.0.1:3000/v1/account/example/submit" \
-H "Authorization: Bearer f77cf263b70488c7a35bf1539fd544a81a88711ff74326ce9793022df08d91b9" \
-H "Content-type: application/json" \
-d '{
"from": {
"name": "Andris Reinman",
"address": "andris@example.com"
},
"to": [
{
"name": "Ethereal",
"address": "andris@ethereal.email"
}
],
"subject": "Test message",
"text": "Hello from myself!",
"html": "<p>Hello from myself!</p>"
}'
https://emailengine.app/sending-emails
邮箱的 POP、SMTP、IMAP 服务器地址设置
- 如何查询某个
邮箱
的发送
邮件服务器地址
和端口
如何查询某个邮箱的接收邮件服务器地址和端口
POP3/SMTP/IMAP服务能让你在本地客户端
上收发邮件
https://mail.163.com/js6/main.jsp
客户端协议
如何开启客户端协议?
什么是POP3、SMTP及IMAP?
IMAP和POP3有什么区别?
Oauth2.0 授权码超过限制怎么办?
https://help.mail.163.com/faq.do?m=list&categoryID=90
什么是
SMTP
认证?
SMTP 的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。
SMTP 认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录 SMTP 服务器,这就使得那些垃圾邮件的散播者无可乘之机。
增加 SMTP 认证的目的是为了使用户避免受到垃圾邮件的侵扰。
https://mail.163.com/mailhelp/client.htm
https://qiye.163.com/help/client-profile.html
163 邮箱 host port
接收 pop
/ imap
接收邮件服务器: http://pop.163.com
接收端口: 110 / 995
(使用 SSL 时)
接收邮件服务器: http://imap.163.com
接收端口: 143 / 993
(使用 SSL 时)
发送 smtp
发送邮件服务器: http://smtp.163.com
发送端口: 25 / 465 / 994
(使用 SSL 时)
Cloudflare Email Routing
电子邮件路由
创建自定义电子邮件地址
,在您不希望共享主要电子邮件地址时使用。
https://developers.cloudflare.com/email-routing/
free email
https://forwardemail.net/zh/private-business-email?pricing=true
refs
https://zhuanlan.zhihu.com/p/618281679
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17667064.html
未经授权禁止转载,违者必究!