"UnhandledPromiseRejectionWarning: Error: Forbidden" while sending email from sendGrid in Node.js

"UnhandledPromiseRejectionWarning: Error: Forbidden" while sending email from sendGrid in Node.js

我正在按照他们网站上的说明使用以下代码将 sendgrid 集成到我的 node.js 项目中

const sgMail = require('@sendgrid/mail')

const sendGridAPIKey = "API key"

sgMail.setApiKey(sendGridAPIKey)

const msg = {
 to: 'agrawalanuj751997@gmail.com',
 from: 'agrawalanuj751997@gmail.com',
 subject:'My first mail from node',
 text:"I'm sending myself an email"
}

sgMail.send(msg)

我在日志中收到以下错误。我尝试了来自多个帐户的多个 API 密钥,但我仍然遇到相同的错误。

(node:16043) UnhandledPromiseRejectionWarning: Error: Forbidden
at Request._callback (node_modules/@sendgrid/client/src/classes/client.js:124:25)
at Request.self.callback (node_modules/request/request.js:185:22)
at Request.emit (events.js:200:13)
at Request.<anonymous> (node_modules/request/request.js:1154:10)
at Request.emit (events.js:200:13)
at IncomingMessage.<anonymous> (node_modules/request/request.js:1076:12)
at Object.onceWrapper (events.js:288:20)
at IncomingMessage.emit (events.js:205:15)
at endReadableNT (_stream_readable.js:1154:12)
at processTicksAndRejections (internal/process/task_queues.js:84:9)
(node:16043) UnhandledPromiseRejectionWarning: Unhandled promise 
rejection. This error originated either by throwing inside of an async 
function without a catch block, or by rejecting a promise which was 
not handled with .catch(). (rejection id: 1)
(node:16043) [DEP0018] DeprecationWarning: Unhandled promise 
rejections are deprecated. In the future, promise rejections that are 
not handled will terminate the Node.js process with a non-zero exit 
code.

我也遇到了类似的问题。我认为他们需要更新文档。 发送方法 returns 一个你没有处理的承诺,这就是你收到错误的原因。

改变

sgMail.send(msg)

sgMail.send(msg).then(() => {
    console.log('Message sent')
}).catch((error) => {
    console.log(error.response.body)
    // console.log(error.response.body.errors[0].message)
})

现在,未处理的 promise 拒绝错误将消失,您将得到 promise 被拒绝的原因。

类似

The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements

这是不言自明的。转到指定的link,它会引导您验证自己的身份。完成后,它应该可以正常工作。


友情链接 https://sendgrid.com/docs/ui/sending-email/sender-verification/

您无法在 sendgrid 上验证用户验证单个发件人

我也遇到了类似的问题。发送方法 returns 一个你没有处理的承诺,这就是你收到错误的原因。

sgMail.send({  
    to: 'arjunregmi148@gmail.com',
    from: 'arjunregmi148@gmail.com',
    subject: 'This is my first creation',
    text:'Be safe from corona virus'
}).then(() => {
    console.log('Message sent')
}).catch((error) => {
    console.log(error.response.body)
})