Anu*_*wal 10 node.js sendgrid visual-studio-code
我正在按照他们网站上的说明使用以下代码在我的 node.js 项目中集成 sendgrid
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)
Run Code Online (Sandbox Code Playgroud)
我的日志中出现以下错误。我已经尝试了多个帐户的多个 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.
Run Code Online (Sandbox Code Playgroud)
Ani*_*iya 18
我也面临着类似的问题。我认为他们需要更新文档。send 方法返回一个您尚未处理的承诺,这就是您收到错误的原因。
改变
sgMail.send(msg)
Run Code Online (Sandbox Code Playgroud)
到
sgMail.send(msg).then(() => {
console.log('Message sent')
}).catch((error) => {
console.log(error.response.body)
// console.log(error.response.body.errors[0].message)
})
Run Code Online (Sandbox Code Playgroud)
现在,未处理的承诺拒绝错误将消失,您将得到承诺被拒绝的原因。
就像是
发件人地址与经过验证的发件人身份不匹配。在解决此错误之前无法发送邮件。访问https://sendgrid.com/docs/for-developers/sending-email/sender-identity/查看发件人身份要求
这是不言自明的。转到指定的链接,它会引导您验证自我身份。完成后,它应该可以正常工作。
链接 https://sendgrid.com/docs/ui/sending-email/sender-verification/
| 归档时间: |
|
| 查看次数: |
4373 次 |
| 最近记录: |