如何从node发送qq邮件
发布于 8 个月前 作者 jowang2016 1011 次浏览 来自 问答

我想使用nodemailer发送邮件 这样配置:

let transporter = nodemailer.createTransport({
  service: 'smtp.exmail.qq.com',
  port: 465,
  secureConnection: true,
  auth: {
    user: '[email protected]',
    pass: 'xxx'
  }
})
let mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: '....'
}
transporter.sendMail(mailOptions, (err, info) => {
    if (err) {
      console.log('err', err)
    }
  })

报错如下:

err { Error: connect ECONNREFUSED 127.0.0.1:465
  at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14)
errno: 'ECONNREFUSED',
code: 'ECONNECTION',
syscall: 'connect',
address: '127.0.0.1',
port: 465,
command: 'CONN' }

为什么服务器会指向 127.0.0.1:465呢?是配置的不对吗?求解答

8 回复

哥们看文档没?

'use strict';
const nodemailer = require('nodemailer');

// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
nodemailer.createTestAccount((err, account) => {
    // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
        host: 'smtp.ethereal.email',
        port: 587,
        secure: false, // true for 465, false for other ports
        auth: {
            user: account.user, // generated ethereal user
            pass: account.pass // generated ethereal password
        }
    });

    // setup email data with unicode symbols
    let mailOptions = {
        from: '"Fred Foo 👻" <[email protected]>', // sender address
        to: '[email protected], [email protected]', // list of receivers
        subject: 'Hello ✔', // Subject line
        text: 'Hello world?', // plain text body
        html: '<b>Hello world?</b>' // 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);
        // Preview only available when sending through an Ethereal account
        console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));

        // Message sent: <[email protected]>
        // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
    });
});

@Yangk030208 使用文档这段代码 给自己邮箱发邮件 打印已经成功发送 但是并没有收到邮件啊~~

大兄弟按照文档是没问题的,可能姿势不对!比如mailOptions里面多填一些东西。在试试

@tzbcf 为啥我死活都收不到邮件呢?帮我看一下呗:

'use strict';
const nodemailer = require('nodemailer');

// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
nodemailer.createTestAccount((err, account) => {
    // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
        host: 'smtp.ethereal.email',
        port: 587,
        secure: false, // true for 465, false for other ports
        auth: {
            user: account.user, // generated ethereal user
            pass: account.pass // generated ethereal password
        }
    });

    // setup email data with unicode symbols
    let mailOptions = {
        from: account.user, // sender address
        to: '[email protected]', // list of receivers
        subject: 'Hello ✔', // Subject line
        text: 'Hello world?', // plain text body
        html: '<b>Hello world?</b>' // html body
    };

    // send mail withs defined transport object
    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
        }
        console.log('Message sent: %s', info.messageId);
        // Preview only available when sending through an Ethereal account
        console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));

        // Message sent: <[email protected]>
        // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
    });
});

@jowang2016 这是我写的。看起来你那也没毛病。主要你还是看下报错信息。两个配置都很正确。就是你那个邮箱的问题了 const options = { from: user, to: req.body.request.to, cc: req.body.request.cc || ‘’, bcc: req.body.request.bcc || ‘’, sender: req.body.request.sender || ‘’, subject: req.body.request.subject || ‘’, html: req.body.request.content || ‘’, encoding: ‘UTF-8’, attachments: req.body.request.attachments || ‘’ }; let config_email = { host: serverAddr, port: serverPort, secure: serverSecure, secureConnection: serverSecure, auth: { user: user, pass: pass, } }; const transporter = nodemailer.createTransport(smtpTransport(config_email)); transporter.sendMail(options, (err, result) => { if (err) { r.errorCode = -6; r.errorMsg = ‘发送失败’; res.send(JSON.stringify®); } else { r.errorCode = 0; r.errorMsg = ‘成功’; res.send(JSON.stringify®); } if (!_.isNull(transporter)) { transporter.close(); transporter = null; } });

@jowang2016 我直接把你的代码拷过来,邮箱地址换成自己的可以的啊

问题解决了 坑在于不同的邮箱 配置不同 之前邮箱配置不对

回到顶部