使用nodemailer / smtp.gmail.com发送电子邮件时出错

时间:2018-09-26 02:58:54

标签: node.js smtp

我正在尝试为应用设置nodemailer。尝试时出现错误。

这是我的设置:

        email = setting.email;
        password = setting.password;

        var transporter = nodemailer.createTransport({
            host: 'smtp.gmail.com',
            port: 587,
            secure: false, // true for 465, false for other ports
            auth: {
                user: '***********@gmail.com', // real email
                pass: '*********' // real password
            }});

        // var transporter = nodemailer.createTransport({
        //     service: 'gmail',
        //     auth: {
        //         user: email, // Your email id
        //         pass: password // Your password
        //     }
        // });

        var mailOptions = {// sender address
            from: email,
            to: to, // list of receivers
            subject: sub, // Subject line
            text: text, //, /// plaintext body
            html: html

        }

        //console.log(JSON.stringify(mailOptions));

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

                console.log(info.response);
            }
            ;
        });
    });

} catch (error) {
    console.error(error);

}

这是我第一次尝试使用nodemailer。我正在使用真实的电子邮件和密码。错误是:

  

(节点:18974)[DEP0025] DeprecationWarning:不建议使用sys。使用工具   代替。魔术发生在端口5000 ERROR上!错误!错误!错误!   provider_analytic_daily已保存。 {错误:连接超时       在SMTPConnection._formatError(/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:528:15)       在SMTPConnection._onError(/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:514:16)       在SMTPConnection。 (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:236:14)       在ontimeout(timers.js:498:11)       在tryOnTimeout(timers.js:323:5)       在Timer.listOnTimeout(timers.js:290:5)代码:“ ETIMEDOUT”,命令:“ CONN”}

1 个答案:

答案 0 :(得分:0)

functionObj = {};

    functionsObj.getSmtpTransport = function() {
        var smtpTransport = nodemailer.createTransport({
            service: "Gmail",
            auth: {
                user: "<your email address>",
                pass: "<your pass>"
            }
        });

        return smtpTransport;
    }

    functionsObj.sendMailForPasswordReset = function(to, token) {

        var smtpTransport = functionsObj.getSmtpTransport();

        var mailOptions = {
            to: to,
            subject: "Blabla.com Reset Password",
            html: "Hi,<br> Click to link for resetting password.<br><a href='https://<blabla.com>/reset/" + urlencode(token) + "'>Click Me !!!</a>"
        }

        smtpTransport.sendMail(mailOptions, function(error, response) {
            if (error) {
                return error;

            }
            else {
                return true;
            }
        });

    };

嗨,

上面的代码在我的项目中正常工作,没有任何错误。但是您应该允许第三方软件使用Google的Google帐户。

在这种情况下,这里是Google的支持页面:

https://support.google.com/accounts/answer/6010255?hl=en

我建议所有输入都应进行验证:)例如,如果您要从用户处获取“ to”参数。您应该验证“收件人”参数是否为有效的电子邮件地址?

相关问题