gpt4 book ai didi

node.js - NodeMailer - G-Suite - 服务账户 - 以群组邮件形式发送

转载 作者:行者123 更新时间:2023-12-05 07:05:53 27 4
gpt4 key购买 nike

问题:

我想通过 Gmail 和服务帐户从群组电子邮件发送电子邮件。但是,当尝试通过 Group@MyDomain.com 而不是 User@MyDomain.com 发送时,我得到:

Error: unauthorized_client
at /Users/fabianbosler/Projects/fb/fb-flow-app/server/node_modules/nodemailer/lib/xoauth2/index.js:264:33
at PassThrough.<anonymous> (/Users/fabianbosler/Projects/fb/fb-flow-app/server/node_modules/nodemailer/lib/xoauth2/index.js:333:20)
at Object.onceWrapper (events.js:421:28)
at PassThrough.emit (events.js:315:20)
at PassThrough.EventEmitter.emit (domain.js:547:15)
at endReadableNT (_stream_readable.js:1201:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EAUTH',
command: 'AUTH XOAUTH2'
}

设置:

我有以下设置:

  1. 具有一个用户的 G-Suite(基本)帐户 (User@MyDomain.com)
  2. 在 Google Cloud Console 和域范围内启用 Gmail API(-> 服务帐户)
  3. 从第 2 步向服务帐户授予相关权限
  4. 组 (Group@MyDomain.com) 其中 User@MyDomain.com 是管理员

此设置可通过服务帐户以 User@MyDomain.com(从第 1 步开始)发送电子邮件:

const conf = require('../config/config');

import nodemailer from 'nodemailer';
import { to } from 'await-to-js';

const SENDER = 'User@MyDomain.com'
const SENDER_NAME = 'User'

const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: SENDER,
serviceClient: conf.GMAIL_CLIENT_ID,
privateKey: conf.GMAIL_CLIENT_PRIVATE_KEY,
},
});

transporter.on('token', (token) => {
console.log('A new access token was generated');
console.log('User: %s', token.user);
console.log('Access Token: %s', token.accessToken);
console.log('Expires: %s', new Date(token.expires));
});

export default async (sendTo, content) => {
return new Promise(async (resolve, reject) => {
let err, verify, result;

const contacts = {
from: {
name: HUMAN_READABLE_NAME,
address: SENDER,
},
to: sendTo,
};

const email = Object.assign({}, content, contacts);

[err, verify] = await to(transporter.verify());
if (err) {
console.log(err);
return reject(`verification failed: ${err}`);
}

[err, result] = await to(transporter.sendMail(email));
if (err) {
console.log(`sending failed: ${err}`);
return reject(err);
}

return resolve(result);
});
};

最佳答案

查看这篇文章中的答案:send a group email address with Nodemailer with G Suite OAuth2

我猜你没有像在邮件中那样设置发送。

关于node.js - NodeMailer - G-Suite - 服务账户 - 以群组邮件形式发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62658352/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com