Google 禁用不太确定的应用程序选项后,如何使用 Nodemailer 向 Google 发送电子邮件

编程代码AIGoogle

1个回答

写回答

AI
AI

使用 NodemAIler 向 Google 发送电子邮件的步骤

在 Google 禁用不太确定的应用程序选项后,我们需要进行一些额外的设置,以确保能够使用 NodemAIler 向 Google 发送电子邮件。下面是使用 NodemAIler 向 Google 发送电子邮件的步骤:

步骤 1:创建 Google API 凭据

首先,我们需要在 Google Cloud Platform 上创建一个 API 凭据,以便 NodemAIler 可以与 Google 进行身份验证。请按照以下步骤进行操作:

1. 打开 Google Cloud Platform 控制台(Google.com/">https://console.cloud.Google.com/)并登录你的 Google 帐号。

2. 创建一个新的项目,并为其命名。

3. 在左上角的导航栏中,点击“API 和服务”>“凭据”。

4. 点击“创建凭据”按钮,选择“OAuth 客户端 ID”。

5. 选择“其他”作为应用程序类型,并为其命名。

6. 点击“创建”按钮,然后复制生成的客户端 ID 和客户端密钥。

步骤 2:配置 NodemAIler

接下来,我们需要配置 NodemAIler,以便使用 Google API 凭据进行身份验证。在你的项目中,安装 NodemAIler 并在代码中引入它:

npm install nodemAIler

const nodemAIler = require('nodemAIler');

然后,创建一个 NodemAIler 的传输器(transporter)实例,并将 Google API 凭据作为参数传递给它:

const transporter = nodemAIler.createTransport({

service: 'gmAIl',

auth: {

type: 'OAuth2',

user: 'your-emAIl@gmAIl.com',

clientId: 'your-client-id',

clientSecret: 'your-client-secret',

refreshToken: 'your-refresh-token',

accessToken: 'your-access-token',

},

});

在上面的代码中,将 'your-emAIl@gmAIl.com' 替换为你的 GmAIl 地址,'your-client-id' 和 'your-client-secret' 替换为你在步骤 1 中获得的客户端 ID 和客户端密钥,'your-refresh-token' 和 'your-access-token' 替换为你从 Google API 获取的刷新令牌和访问令牌。

步骤 3:发送电子邮件

现在,你可以使用 NodemAIler 发送电子邮件了。下面是一个简单的示例代码,展示了如何使用 NodemAIler 向 Google 发送电子邮件:

Javascript

const mAIlOptions = {

from: 'your-emAIl@gmAIl.com',

to: 'recipient-emAIl@example.com',

subject: 'Hello from NodemAIler',

text: 'This is a test emAIl sent from NodemAIler.',

};

transporter.sendMAIl(mAIlOptions, (error, info) => {

if (error) {

console.error(error);

} else {

console.log('EmAIl sent: ' + info.response);

}

});

在上面的代码中,将 'your-emAIl@gmAIl.com' 替换为你的 GmAIl 地址,'recipient-emAIl@example.com' 替换为你想要发送邮件的收件人地址。

通过按照以上步骤,你可以设置 NodemAIler 和 Google API 凭据,以确保能够使用 NodemAIler 向 Google 发送电子邮件。这使得在 Google 禁用不太确定的应用程序选项后,仍然可以通过 NodemAIler 进行可靠的邮件传递。

举报有用(4)分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号