javascript - 在 Node.js 中使用 Azure SendGrid 发送电子邮件

标签 javascript node.js azure sendgrid

我正在关注the documentation为了使用 SendGrid 发送电子邮件:

var sendgrid = require('sendgrid')("username", "passowrd");

var email = new sendgrid.Email({
    to: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="66131503142605090b1607081f4805090b" rel="noreferrer noopener nofollow">[email protected]</a>',
    from: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89fcfaecfbc9eae6e4f9e8e7f0a7eae6e4" rel="noreferrer noopener nofollow">[email protected]</a>',
    subject: 'test mail',
    text: 'This is a sample email message.',
    html: 'This is a sample <b>HTML<b> email message.'
});

sendgrid.send(email, function(err, json){
    if(err) { return console.error(err); }
    console.log(json);
});

但我收到错误:

sendgrid.Email is not a constructor

最佳答案

我在各种项目中使用sendgrid,我一直在使用https://github.com/sendgrid/sendgrid-nodejs中的文档。 我想知道 Azure 的文档是否仍然是最新的...

只要您使用@sendgrid/mail 软件包并正确配置您的帐户,就可以。此代码应该有效:

const createSampleMessage = {
  to: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56232533241635393b2637382f7835393b" rel="noreferrer noopener nofollow">[email protected]</a>',
  from: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a2f293f281a3935372a3b342374393537" rel="noreferrer noopener nofollow">[email protected]</a>',
  subject: 'test mail',
  text: 'This is a sample email message.',
  html: 'This is a sample <b>HTML<b> email message.'
};
if (process.env.NODE_ENV !== "test") {
  await sgMail.send(createSampleMessage);
}

关于javascript - 在 Node.js 中使用 Azure SendGrid 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54128060/

相关文章:

node.js - 在 sequelize 中搜索 (column1 + column2 +column3) <= column4

c# - 如何使用 Microsoft Graph 创建 Microsoft Teams session 作为应用程序?

c# - 更新到VS 2015、Azure 2.7,发布失败 'Access to path ... denied'

javascript - 添加连接第二个可排序列表的条件

javascript - Twitter Api 通过主题标签搜索

json - 根据请求的内容提供 JSON 或 HTML?

Node.js 在服务器和客户端之间同步音频流?

javascript - wait/async 如何处理 Unresolved promise

javascript - 在 Backbone.js 中创建自定义 "sync"方法

azure - 即使明确信任,Windows 应用商店应用程序是否始终不允许自签名证书?