错误:SMTPConnection._formatError 连接超时

Cod*_*Bae 4 javascript email mongodb node.js express

错误:SMTPConnection._formatError 连接超时,不知道出了什么问题,无法发送邮件,请帮助我。我正在尝试使用 nodemailer 发送邮件,但我不断在控制台中收到此错误

    Error: Connection timeout
        at SMTPConnection._formatError (/home/codabae/Desktop/mailmonster/Backend/v1/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
        at listOnTimeout (internal/timers.js:531:17)
        at processTimers (internal/timers.js:475:7) {
      code: 'ETIMEDOUT',
      command: 'CONN'
    }
Run Code Online (Sandbox Code Playgroud)

这是我的 api,我不知道做错了什么,我从 mongodb 获取详细信息并将其填充到 nodmailer 字段中,我真的不知道做错了什么。

    router.post('/', auth, (req, res) => {
    const { to, cc, bcc, subject, message, attachment, smtpDetails } = req.body;

    if (!to || !subject || !message || !smtpDetails) return res.status(400).send('input cannot be empty')

    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: '...@gmail.com',
            pass: '...'
        }
    });

    let mailOptions = {
        from:  '...@gmail.com',
        to: to,
        cc: cc,
        bcc: bcc,
        subject: subject,
        text: `${message}`
    };

    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log(error);
            res.send('mail not sent')

        } else {
            console.log('Email sent: ' + info.response);
            res.send('mail sent')
        }
    });    

    module.exports = router;
Run Code Online (Sandbox Code Playgroud)

kil*_*t13 7

这里要重点关注的错误消息部分是SMTPConnection._formatError。您收到此错误是因为传输配置变量不正确。您需要以下变量,并且每个变量都有正确的值。

还可以使用其他变量,但根据代码中的字段,以下配置应该可以正常工作。如果您需要其他信息,可以随时参考Nodemailer 文档

//transport configuration for user a site server to send an email.
let transporter = nodemailer.createTransport({
    // This is the SMTP mail server to use for notifications. 
    // GCDS uses this mail server as a relay host.
    host: "smtp.gmail.com",
    // SMTP is unlike most network protocols, which only have a single port number. 
    // SMTP has at least 3. They are port numbers 25, 587, and 465.
    // Port 25 is still widely used as a **relay** port from one server to another.
    // Port for SSL: 465
    // Port for TLS/STARTTLS: 587
    port: 465,
    //  if true the connection will use TLS when connecting to server. If false (the 
    // default) then TLS is used if server supports the STARTTLS extension. In most 
    // cases set this value to true if you are connecting to port 465. For port 587 or 
    // 25 keep it false
    secure: true, // use TLS
    auth: {
        // Your full email address
        user: process.env.SMTP_TO_EMAIL,
        // Your Gmail password or App Password
        pass: process.env.SMTP_TO_PASSWORD
    }
});
Run Code Online (Sandbox Code Playgroud)

您提到使用 MongoDB 中的某种信息来填充这些内容。我看不到您从哪里获取配置中的变量和值,但如果您从数据库中提取值,则可能需要使用不同的源或更新查询。

注意: 另一个需要注意的常见问题涉及使用 Gmail 密码,如果您的帐户启用了 2FA,则无法使用该密码。在这种情况下,您必须按照 Google 的这些指南生成唯一的应用程序密码。