«UnhandledPromiseRejectionWarning: Ошибка: запрещено» при отправке электронной почты из sendGrid в Node.js - PullRequest
2 голосов
/ 12 апреля 2020

Я интегрирую 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.

Ответы [ 2 ]

3 голосов
/ 13 апреля 2020

Я также столкнулся с подобной проблемой. Я думаю, что они должны обновить документы. Метод send возвращает обещание, которое вы не обработали, поэтому вы получаете сообщение об ошибке.

измените

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)
})

Сейчас , ошибка отклонения необработанного обещания пропадет, и вы получите ошибку, почему обещание было отклонено.

Что-то вроде

Адрес отправителя не соответствует проверенному отправителю Идентичность. Почта не может быть отправлена, пока эта ошибка не будет устранена. Посетите https://sendgrid.com/docs/for-developers/sending-email/sender-identity/, чтобы увидеть требования к Идентификации отправителя

Это довольно очевидно. Go на указанную ссылку, и она поможет вам подтвердить свою личность. Как только вы завершите это, он должен работать нормально.


Ссылки https://sendgrid.com/docs/ui/sending-email/sender-verification/

0 голосов
/ 06 мая 2020

вы не можете подтвердить, что пользователь подтвердил одного отправителя в sendgrid

...