nodemailer [错误:未定义传输方法]

时间:2016-06-15 20:13:10

标签: node.js express sendgrid nodemailer cloud9

因此,我使用cloud9作为IDE,nodejs,express和nodemailer@0.7.1以及nodemailer-sendgrid-transport。这是我的代码:

var nodemailer=require('nodemailer');
var sgTransporter = require('nodemailer-sendgrid-transport');
var options={
    auth: {
        api_user: "xxx",
        api_pass: "xxx"
    }
};
var client = nodemailer.createTransport(sgTransporter(options));

var sendMail=function(emailTo, subject, html, fn){
    var mailOptions={
        from:'xxx',
        to:emailTo,
        subject:subject,
        html:html
    };

    client.sendMail(mailOptions, function(error, response){
        if(error){
            console.log(error);
            fn(error);
        }
        else{
            console.log("Message sent: " + response.message);
            fn(response);
        }
    });
};

这里调用sendMail函数:

router.get('/verifyEmail', function(req,res){
    if(req.isAuthenticated()) {
        var subject='Verify your email address';
        var link="https://myapp-myuser.c9users.io/verify/:"+req.user._id;
        var html='Hello,<br> Please Click on the link to verify your Account.<br><a href='+link+'>Click here to verify</a>';
        console.log('Sending verification email to: '+req.user.email);
        sendMail(req.user.email,subject, html,function(error, response){
            if (error){
                return res.status(400).json({error:error});
            }
            return res.status(200).json(response);
        });
    }
});

但是电子邮件没有发送......我收到以下错误: [Error: No transport method defined]

我尝试了很多不同的解决方案。我看待它的方式,我做的与本例中完全相同:https://sendgrid.com/blog/sending-email-nodemailer-sendgrid/这可能是一个cloud9问题吗?我也尝试过使用Gmail帐户。允许不安全的应用程序后出现相同的错误...

编辑:好的,我已将nodemailer升级到版本2.4.2并抛出新错误... [Error: The provided authorization grant is invalid, expired, or revoked]

1 个答案:

答案 0 :(得分:0)

大多数时候......这是小事,天哪,我被困在这...... 问题是:

auth: {
    api_user: "xxx",
    api_pass: "xxx"
}

应该是这样写的:

auth: {
    api_user: "xxx",
    api_key: "xxx"
}

那,以及nodemailer的版本......