前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Node.js 测试 SMTP 服务

Node.js 测试 SMTP 服务

作者头像
为为为什么
发布2022-08-06 17:03:31
发布2022-08-06 17:03:31
1.4K00
代码可运行
举报
文章被收录于专栏:又见苍岚又见苍岚
运行总次数:0
代码可运行

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

Node.js 测试 SMTP

  • node 脚本可以使用 node xxx.js 执行
  • 如果有包找不到,可以使用命令安装
代码语言:javascript
代码运行次数:0
运行
复制
npm install --save <package_name>

方法一

  • 使用 net 包,建立连接
  • 示例代码(账号使用的是测试账号,可以直接运行)
代码语言:javascript
代码运行次数:0
运行
复制
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('连接断开')
})

  • 直接运行,输出结果:
代码语言:javascript
代码运行次数:0
运行
复制
接受到: 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

  • 登录邮箱: 101.43.39.125
  • 成功收到测试邮件

方法二

当前支持的服务 "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 服务并申请授权码)
代码语言:javascript
代码运行次数:0
运行
复制
'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)
});

  • 成功收到邮件:

参考资料

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年2月6日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Node.js 测试 SMTP
  • 方法一
  • 方法二
  • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档