node.js - 无法使用sendgrid发送电子邮件

标签 node.js sendgrid

   // this is my code
//unable to send email using sendgrid
// finding email from body
    exports.signUp=async (req,res)=>{
        const userExists = await User.findOne({ email: req.body.email })
        if(userExists){
            return res.status(400).json({
                error:"eamil is already taken"
            })
        }
        const token = jwt.sign(req.body, process.env.ACCOUNT_ACTIVATION,{expiresIn:'20m'})
        const emailSend = {
            from: process.env.EMAIL_FROM,
            to: req.body.email,
            subject:`Acoount activation link`,
            html:`
                <p> Please click the following link to activate</p>
    
                <a>http://localhost:8000/api/auth/activate/${token}</a>
            `
        }
        sgMail.send(emailSend).then(sent=>{
            return res.status(200).json({
                message:'email has been sent to you emailid'
            })
        })
        .catch(err=>console.log(err))
    }
    
    
    
   // this is error am getting
// am getting this error 
    
   

ResponseError: Forbidden at D:\node-projects\ecom\node_modules@sendgrid\client\src\classes\client.js:146:29 at processTicksAndRejections (internal/process/task_queues.js:93:5) {
code: 403, response: { headers: { server: 'nginx', date: 'Thu, 07 Jan 2021 11:55:26 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' }, body: { errors: [Array] } } }

最佳答案

日志显示失败,因为它收到了 Forbidden html 响应。
尝试检查您是否在 SendGrid 设置中设置了发件人身份验证
只有完成此设置后,您才能发送电子邮件。

引用文献:https://sendgrid.com/docs/for-developers/sending-email/sender-identity/

关于node.js - 无法使用sendgrid发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65612030/

相关文章:

javascript - 将数据发送到特定客户端而不是整个 channel

python - Heroku 上 Django 应用程序中的 SMTP-Sendgrid 电子邮件 : working locally, 错误 SMTPServerDisconnected

c# - 使用 SendGrid 安排邮件

c# - Sendgrid API : The provided authorization grant is invalid, 已过期或已撤销

javascript - async/await 捕获被拒绝的 Promise

node.js - Mongoose 更新深度数组

node.js - 通过 nodejs 和 websockets 将功能从 openlayers 保存到 postgis

node.js - 什么是everyauth promise ?

node.js - 从Node.js中的sendGrid发送电子邮件时"UnhandledPromiseRejectionWarning: Error: Forbidden"

java - 创建了 sendgrid 电子邮件模板,如何从 Java 调用它