【问题标题】:Email NodeJS cannot send电子邮件 NodeJS 无法发送
【发布时间】:2019-03-03 12:20:24
【问题描述】:

我想从 NodeJs 发送一封电子邮件。我在 npmjs.org 上进行了快速搜索,发现了 2 个有前途的库:

Nodemailer

EmailJs

首先,我按照说明安装和运行 NodeMailer,但它不起作用。我想从 Gmail 发送电子邮件,因此,我启用了 "less secured" 应用程序和 enabled captcha,正如 Nodemailer 的文档中所建议的那样。但是,即使我这样做了,我也无法让它发挥作用。

另外,我尝试切换到我大学的电子邮件,所以我找到了它的 SMTP 服务器和端口并配置了设置。错误依旧,主要是:

(node:825) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 1): Error: connect EAGAIN 134.91.56.35:587 - Local (0.0.0.0:58442)
(node:825) [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.

我不确定如何修复它,所以我尝试了下一个客户端,EmailJs。错误是:

{ Error: connection encountered an error (connect EAGAIN 134.91.56.35:587 ...
code: 5,
previous: ...
... smtp: undefied}

我可以清楚的看到写的是smtp是未定义的,但是我已经验证是一样的。

为了澄清,我在 Windows 上的 Bash 上运行 NodeJs,但由于我知道它存在一些问题,所以我也在 Raspberry PI 3 上运行代码。

谢谢!

【问题讨论】:

    标签: node.js email


    【解决方案1】:

    我设法配置它。

    按照建议,我使用了Gmail-node。为此,您必须遵循一些步骤:

    • 首先,使用npm install gmail-node安装它
    • 打开链接https://console.developers.google.com/flows/enableapi?apiid=gmail
    • 使用此向导在 Google Developers 中创建或选择一个项目 控制台并自动开启 API。
    • 单击继续,然后转到 凭据。
    • 在页面顶部,选择 OAuth 同意屏幕 标签。选择一个电子邮件地址,如果还没有,请输入产品名称 设置,然后点击保存按钮。
    • 选择凭据选项卡,单击 创建凭据按钮并选择 OAuth 客户端 ID。
    • 选择应用类型Other,输入任意名称“gmail-node-app”,然后 点击创建按钮。
    • 单击“确定”关闭生成的对话框。
    • 点击右侧的file_download(下载JSON)按钮 客户 ID。
    • 将此文件移动到您的工作目录并与 任何名称,例如 client_secret.json。

    现在您的程序将如下所示,您必须根据刚刚下载的文件更改信息:

    var gmailNode = require('gmail-node');
    var clientSecret = {
        installed: {
            client_id: "k677725446467-6li25pcqgkcllsoh6f6dijcvse64n9pf.apps.googleusercontent.com",
            project_id: "clean-node-129606",
            auth_uri: "https://accounts.google.com/o/oauth2/auth",
            token_uri: "https://accounts.google.com/o/oauth2/token",
            auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
            client_secret: "kF7DvoA_ZrNfa65GnU2zQBgw7",
            redirect_uris: [
                "urn:ietf:wg:oauth:2.0:oob",
                "http://localhost"
            ]
        }
    };
    gmailNode.init(clientSecret, './token.json', function(err,data){ ... });
    

    但是,从网站上拿了这个之后,我仍然有一个未知的问题,即Error: No access or refresh token is set.

    为了解决它,您需要通过记录此代码生成带有“TokenURL”的url:

    console.log(gmailNode.generateUrl(clientSecret));
    

    按照生成的 URL 并在那里复制“TokenURL”。

    最后,发送一封电子邮件:

    gmailNode.generateToken(clientSecret, tokenURL, (err, data) => {
            gmailNode.sendWithToken(emailMessage, clientSecret, data, function(err,data) {
                    console.log(err, data)
            })
    })
    

    你就完成了!

    请注意,我们不再需要 init

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2013-09-22
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 2020-05-30
      • 2019-01-19
      • 2013-06-07
      相关资源
      最近更新 更多