Nodemailer 电子邮件发送在 Node.js 中不起作用

Nodemailer e-mail send not working in Node.js

我尝试使用 Node.js Nodemailer 发送电子邮件,但它不起作用。我不知道如何正确设置它。我使用了下面的代码,但它不起作用并且出现错误。我是 Nodemailer 的新手,所以我无法找出错误。

允许安全性较低的应用程序:开启

我需要从 google 帐户做什么?请帮助任何人找到解决方案。

app.js:

const express = require('express');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const path = require('path');
const nodemailer = require('nodemailer');

const app = express();

// View engine setup
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');

// Static folder
app.use('/public', express.static(path.join(__dirname, 'public')));

// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.engine('handlebars', exphbs({
    defaultLayout: false,
}));

//app.locals.layout = false;
 
app.get('/', (req, res) => {
    res.render('contact');
});

app.post('/send', (req, res) => {
    const output = `
    <p>You have a new contact request</p>
    <h3>Contact Details</h3>
    <ul>  
      <li>Name: ${req.body.name}</li>
      <li>Company: ${req.body.company}</li>
      <li>Email: ${req.body.email}</li>
      <li>Phone: ${req.body.phone}</li>
    </ul>
    <h3>Message</h3>
    <p>${req.body.message}</p>
  `;

    // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
        host: 'mail.madepartooty.com',
        port: 587,
        secure: false, // true for 465, false for other ports
        auth: {
            user: 'myemail@gmail.com', // generated ethereal user
            pass: 'test123' // generated ethereal password
        },
        tls: {
            rejectUnauthorized: false
        }
    });

    // setup email data with unicode symbols
    let mailOptions = {
        from: '"Nodemailer Contact" <myemail@gmail.com>', // sender address
        to: 'toemail@gmail.com', // list of receivers
        subject: 'Node Contact Request', // Subject line
        text: 'Hello world?', // plain text body
        html: output // 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('Preview URL: %s', nodemailer.getTestMessageUrl(info));
        res.render('contact', { msg: 'Email has been sent' });
    });
});

app.listen(3000, () => console.log('Server started...'));

错误:

Error: connect ETIMEDOUT 157.245.154.37:587
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  errno: 'ETIMEDOUT',
  code: 'ECONNECTION',
  syscall: 'connect',
  address: '157.245.154.37',
  port: 587,
  command: 'CONN'
}

这是由于在给定时间内(通过超时请求模块选项)未收到您的请求响应造成的。您可以设置请求的 timeout 以正确执行您的请求并获得响应。

您也可以通过这种方式构建错误处理程序:

process.on('uncaughtException', (err, origin) => {
  
});

您可以执行以下操作

app.js

var nodemailer = require('nodemailer');
var tech = require('./external.js');

var transporter = nodemailer.createTransport({
 service: 'Gmail',
 auth: {
        user: tech.senderUsername,
        pass: tech.senderPassword
    }
});

const mailOptions = {
  from: tech.senderUsername, // sender address
  to: tech.reciverUsername, // list of receivers
  subject: 'Subject of your email', // Subject line
  html: '<p>Enter your message here.</p>'// plain text body
};

transporter.sendMail(mailOptions, function (err, info) {
   if(err)
     console.log(err)
   else
     console.log(info);
});

注:

您在使用 google 帐户时可能还需要执行这些步骤

启用设置以允许您正在使用的 Gmail 帐户使用安全性较低的应用程序。 这是 link:Google Less Secure Apps

允许访问“显示解锁验证码选项”(允许访问您的 Google 帐户) 这是link:Google Unlock Captcha

更多信息,您可以浏览sendMailUsingNodemailer