zoukankan      html  css  js  c++  java
  • nodeJS基于smtp发邮件

    邮件的协议smtp是tcp/ip族中的一个协议,所以我们这次考虑使用net模块来发送邮件。

    const net = require('net')
    const assert = require('assert')
    
    const host = 'smtp.163.com',
          port = 25,
          user = 'zhangjunyi199616@163.com',
          pass = '*********',
          to = '442665319@qq.com',
          subject = '少林武当大叮当',
          msg = `你好,张啊咩同学,我是一封来自node的邮件
          
          爱你哟123
          `
    
    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}
    `)
        sendData(`${msg}
    .`)
    
        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+'
    ')
    }
    
    let data = null
    client.on('data', d => {
        console.log('接受到:'+d.toString())
        data = d.toString().substring(0,3)
    })
    client.on('end', () => {
        console.log('连接断开')
    })

    过程:

    连接上了
    接受到:220 163.com Anti-spam GT for Coremail System (163com[20141201])
    
    发送:HELO smtp.163.com
    接受到:250 OK
    
    发送:auth login
    接受到:334 dXNlcm5hbWU6
    
    发送:emhhbmdqdW55aTE5OTYxNkAxNjMuY29t
    接受到:334 UGFzc3dvcmQ6
    
    发送:emhhbmc4NjQ3NzY2MA==
    接受到:235 Authentication successful
    
    发送:MAIL FROM:<zhangjunyi199616@163.com>
    接受到:250 Mail OK
    
    发送:RCPT TO:<442665319@qq.com>
    接受到:250 Mail OK
    
    发送:DATA
    接受到:354 End data with <CR><LF>.<CR><LF>
    
    发送:SUBJECT:少林武当大叮当
    发送:FROM:zhangjunyi199616@163.com
    发送:TO:442665319@qq.com
    
    发送:你好,张啊咩同学,我是一封来自node的邮件
    
          爱你哟123
    
    .
    接受到:250 Mail OK queued as smtp7,C8CowAC3Xh8MErdaVr+lLw--.64772S2 1521947148
    
    发送:QUIT
    接受到:221 Bye
    
    连接断开
  • 相关阅读:
    基础最短路(模板 bellman_ford)
    UVA-12304 Race(递推)
    How do you add?(递推)
    Coconuts, Revisited(递推+枚举+模拟)
    UVA-10726 Coco Monkey(递推)
    UVA-10995 Educational Journey
    UVA-10339 Watching Watches
    【React】377- 实现 React 中的状态自动保存
    【JS】376- Axios 使用指南
    【Nodejs】375- 如何加快 Node.js 应用的启动速度
  • 原文地址:https://www.cnblogs.com/amiezhang/p/8643436.html
Copyright © 2011-2022 走看看