节点 JS:Sendgrid 邮件 403“禁止”错误

ale*_*dap 13 email node.js http-status-code-403 sendgrid express

我在 node js 项目中使用 Send grid 发送了一封非常简单的电子邮件。但是我得到了一个 403 Forbidden 错误。API 密钥具有完全访问权限。该代码也已正确集成,因为我使用了另一个帐户的另一个 API 密钥,并且它运行良好。

错误日志:

在此处输入图片说明

有什么建议?

小智 15

该错误存在的原因是消息中“发件人”字段中的电子邮件地址(在您的 nodejs 代码中,将使用 sendgrid 发送)未通过 sendgrid 验证。仅将该电子邮件地址放在由 sendgrid 明确验证的“发件人”字段中。

要验证您的发件人电子邮件地址以便能够发送电子邮件,请参阅以下链接:-

https://sendgrid.com/docs/ui/sending-email/sender-verification

希望这可以帮助。

(可能有关于域名的更多问题,请正确阅读链接,他们有关于使用 gmail.com 地址的警告,您可以忽略)

  • 这应该是公认的答案,谢谢! (6认同)

Jon*_*hez 8

将整个错误粘贴到此处

ResponseError: Forbidden
    at node_modules/@sendgrid/client/src/classes/client.js:133:29
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  code: 403,
  response: {
    headers: {
      server: 'nginx',
      date: 'Sat, 10 Oct 2020 17:22:02 GMT',
      'content-type': 'application/json',
      'content-length': '281',
      connection: 'close',
      'access-control-allow-origin': 'https://sendgrid.api-docs.io',
      'access-control-allow-methods': 'POST',
      'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
      'access-control-max-age': '600',
      'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html'
    },
Run Code Online (Sandbox Code Playgroud)

这是进一步扩展@Aman 上面回答的解决方案。

  • 您必须验证您发送邮件的电子邮件地址。

所以意思是from: address这里

const msg = {
    to: 'recepient@email.com',
    from: 'sender@email.com', //this is the address that needs to be verified by sendgrid
    subject: 'Sending from Sendgrid',
    text: 'here is the test from node',
    html: `<strong> Here is the order #${orderNumber} user: ${user} </strong>`,
}
Run Code Online (Sandbox Code Playgroud)

以下是如何验证它 https://sendgrid.com/docs/ui/sending-email/sender-verification/

请参阅下面的屏幕截图:

在此输入图像描述


小智 5

from检查您验证发送地址时是否使用了电子邮件地址。


Sea*_*n C -3

[Tulio Faria] 通过在他的 Sendgrid 帐户上执行“单一发件人身份验证”解决了这个问题。

不久前我也遇到了类似的问题。帮助我解决这个问题的是转到设置> API 密钥,然后将我正在使用的密钥的设置更改为完全访问权限。 在此输入图像描述

  • @sean - 这个解决方案对于这里真正发生的事情来说太过分了,错误是因为你的 msg 对象中的 `from: 'emailAddress@email.com',` 尚未被 Sendgrid 确认/验证 (2认同)