本文最后更新于:2023年12月5日 下午

本文记录 Node.js 脚本测试 SMTP 的两种方法。

Node.js 测试 SMTP

  • node 脚本可以使用 node xxx.js 执行
  • 如果有包找不到,可以使用命令安装
1
npm install --save <package_name>

方法一

  • 使用 net 包,建立连接
  • 示例代码(账号使用的是测试账号,可以直接运行)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const net = require('net')
const assert = require('assert')

const host = 'smtp.zywvvd.com',
port = 25,
user = 'test@zywvvd.com',
pass = '12345678',
to = 'test@zywvvd.com',
subject = '测试邮件',
msg = `这是一封来自node的邮件`

let client = net.createConnection({host,port},async() => {
console.log('连接上了')
let code
code = await getData()
assert(code == 220)
// 打招呼
sendData('HELO ' + host)

code = await getData()
assert(code == 250)
// 要登陆
sendData('auth login')

code = await getData()
assert(code == 334)
// 给用户名(邮箱)---base64编码
sendData(new Buffer(user).toString('base64'))

code = await getData()
assert(code == 334)
// 给密码---base64编码
sendData(new Buffer(pass).toString('base64'))

code = await getData()
assert(code == 235)
// 给用户名(邮箱
sendData(`MAIL FROM:<${user}>`)

code = await getData()
assert(code == 250)
// 给目标邮箱
sendData(`RCPT TO:<${to}>`)

code = await getData()
assert(code == 250)
// 要发送数据
sendData('DATA')

code = await getData()
assert(code == 354)
// 发主题
sendData(`SUBJECT:${subject}`)
// 发发件人
sendData(`FROM:${user}`)
// 发目标
sendData(`TO:${to}\r\n`)
sendData(`${msg}\r\n.`)

code = await getData()
sendData(`QUIT`)

})

function getData() {
return new Promise((resolve,reject) => {
next()
function next(){
if(data) {
let temp = data
data =null
resolve(temp)
} else {
setTimeout(next,0)
}
}
})
}

function sendData(msg) {
console.log('发送: '+msg)
client.write(msg+'\r\n')
}

let data = null
client.on('data', d => {
console.log('接受到: '+d.toString())
data = d.toString().substring(0,3)
})
client.on('end', () => {
console.log('连接断开')
})
  • 直接运行,输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
接受到: 334 VXNlcm5hbWU6

test.js:87
发送: dGVzdEB6eXd2dmQuY29t
test.js:81
(node:13692) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
test.js:28
接受到: 334 UGFzc3dvcmQ6

test.js:87
发送: MTIzNDU2Nzg=
test.js:81
接受到: 235 2.7.0 Authentication successful

test.js:87
发送: MAIL FROM:<test@zywvvd.com>
test.js:81
接受到: 250 2.1.0 Ok

test.js:87
发送: RCPT TO:<test@zywvvd.com>
test.js:81
接受到: 250 2.1.5 Ok

test.js:87
发送: DATA
test.js:81
接受到: 354 End data with <CR><LF>.<CR><LF>

test.js:87
发送: SUBJECT:测试邮件
test.js:81
发送: FROM:test@zywvvd.com
test.js:81
发送: TO:test@zywvvd.com

test.js:81
发送: 这是一封来自node的邮件
.
test.js:81
接受到: 250 2.0.0 Ok: queued as 6FB8F11E79F

test.js:87
发送: QUIT
test.js:81
接受到: 221 2.0.0 Bye

test.js:87
连接断开
test.js:91

  • 成功收到测试邮件

方法二

当前支持的服务 "126", "163", "1und1", "AOL", "DebugMail", "DynectEmail", "FastMail", "GandiMail", "Gmail", "Godaddy", "GodaddyAsia", "GodaddyEurope", "hot.ee", "Hotmail", "iCloud", "mail.ee", "Mail.ru", "Maildev", "Mailgun", "Mailjet", "Mailosaur", "Mandrill", "Naver", "OpenMailBox", "Outlook365", "Postmark", "QQ", "QQex", "SendCloud", "SendGrid", "SendinBlue", "SendPulse", "SES", "SES-US-EAST-1", "SES-US-WEST-2", "SES-EU-WEST-1", "Sparkpost", "Yahoo", "Yandex", "Zoho", "qiye.aliyun"
  • 示例代码(以QQ为例,需要去QQ邮箱开启 IMAP/SMTP 服务并申请授权码)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
// host: 'smtp.ethereal.email',
service: 'qq', // 使用了内置传输发送邮件 查看支持列表:https://nodemailer.com/smtp/well-known/
port: 465, // SMTP 端口
secureConnection: true, // 使用了 SSL
auth: {
user: 'xxxxxxx@qq.com',
// 这里密码不是qq密码,是你设置的smtp授权码,去qq邮箱后台开通、查看
pass: 'xxxxxxxxxxxx',
}
});

let mailOptions = {
from: '"Rogn" <xxxxxxx@qq.com>', // sender address
to: 'xxxxxxx@qq.com', // list of receivers
subject: 'Hello', // Subject line
// 发送text或者html格式
// text: 'Hello world?', // plain text body
html: '<h1>Hello world</h1>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
// console.log('Message sent: %s', info.messageId);
console.log(info)
});
  • 成功收到邮件:

参考资料



文章链接:
https://www.zywvvd.com/notes/coding/node-js/node-js-smtp/node-smtp/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Node.js 测试 SMTP 服务
https://www.zywvvd.com/notes/coding/node-js/node-js-smtp/node-smtp/
作者
Yiwei Zhang
发布于
2022年2月6日
许可协议