如何使用sendgrid检查节点js中是否存在电子邮件

问题描述

我想检查注册电子邮件是否有效。 (这里的“有效”表示电子邮件存在。)我想如果我从 SendGrid 邮件程序收到结果,那么我可以看到它。但是当我将消息发送到注册电子邮件时,即使该电子邮件不存在,结果也总是成功。下面是我的代码,我总是得到状态 202 的结果,而不是出现错误。 如果有人解决这个问题或教我另一种验证方法,我将不胜感激。

const sgMail = require("@sendgrid/mail");
require("dotenv").config();

sgMail.setApiKey(process.env.SENDGRID_API_KEY);

const sendEmail = async (options) => {
  const msg = {
    from: process.env.FROM_EMAIL,to: options.email,subject: options.subject,text: options.message,html: options.html,};

  console.log(msg);
  try {
    let result = await sgMail.send(msg);
    return true;
  } catch (err) {
    console.log("email sending error ==> ",err.response.body);
    return false;
  }
};

export { sendEmail };

解决方法

Sendgrid 有一个单独的 api 用于电子邮件验证。你可以找到它 here

可以在 here 中找到请求和响应的语法。