带有 apikey 的 Nodemailer / Sendgrid

Nodemailer / Sendgrid with apikey

我正在尝试使用 apikey 通过 sendgrid 在 node.js 中获取一个简单的联系表单和 运行。不清楚哪里错了。

我尝试了以下方法: 1.

var options = {
    auth: {
        api_user: 'xjh-4XqZGH6$HLT-IEOPFG',
        api_key: 'SG.xjh-4XqZGH6$HLT-IEOPFG.Wl2rB-CN00-nj3x5NiKno7MpDk8DxTtvgJeZfDGGI' // fake obviously, but I would like to show the structure as I am not sure whether this is correct
    }
};

和 2.

var options = {
    auth: {
        api_user: 'xjh-4XqZGH6$HLT-IEOPFG',
        api_key: 'Wl2rB-CN00-nj3x5NiKno7MpDk8DxTtvgJeZfDGGI' // fake obviously, but I would like to show the structure as I am not sure whether this is correct
    }
};

和 3.

var options = {
    auth: {
        api_user: 'apikey',
        api_key: 'SG.xnh-4XqZGH6$HLT-IEOPFG.Wl2rB-CN00-nj3x5NiKno7MpDk8DxTtvgJeZfDGGI' // fake obviously, but I would like to show the structure as I am not sure whether this is correct
    }
};

它不断返回错误的用户名/密码。

我的用户名 (api_user) 和密码 (api_key) 可以正常使用。

谁能解释一下我需要做什么?

干杯,迈克

好的,我明白了。在这种情况下,您删除 api_user 并使用长键。例如

var options = {
auth: {
    api_key: 'SG.xnh-4XqZGH6$HLT-IEOPFG.Wl2rB-CN00-nj3x5NiKno7MpDk8DxTtvgJeZfDGGI' // fake
}

};

最好的,迈克

对于遇到此问题的任何人:现在首选的方法似乎是安装 nodemailer-sendgrid package which allows you to set an apiKey property - see my answer here

您可以同时使用 SMTP 和 API 密钥:

const mailer = nodemailer.createTransport({
    host: 'smtp.sendgrid.net',
    port: 465,
    secure: true,
    auth: {
        user: 'apikey',
        pass: 'SG.XXXX.XXXX',
    },
});

官方文档::https://sendgrid.com/docs/API_Reference/SMTP_API/integrating_with_the_smtp_api.html