nodemailer [Error: No transport method defined]

nodemailer [Error: No transport method defined]

所以,我使用 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]

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

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

应该这样写:

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

那个,还有nodemailer的版本...