You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Apps Script中指向其他Gmail账户/文件夹

访问非主邮箱的Gmail账户

要在Google Apps Script中访问你有权限的其他公司邮箱,核心是利用GmailApp方法的可选userId参数——前提是你的主账户已获得目标邮箱的访问权限(比如公司管理员配置的邮箱委托、共享邮箱权限)。

具体实现

1. 获取目标邮箱的收件箱会话

直接给getInboxThreads等方法传入userId参数,指定目标邮箱地址:

// 替换为你的目标邮箱地址
const targetEmail = "department-shared@yourcompany.com";

// 获取目标邮箱的前10条收件箱会话
const threads = GmailApp.getInboxThreads(0, 10, {userId: targetEmail});

// 遍历会话示例
threads.forEach(thread => {
  console.log(`会话主题: ${thread.getFirstMessageSubject()}`);
});

2. 操作目标邮箱的自定义文件夹(标签)

如果要访问目标邮箱的特定文件夹,可先通过getUserByName获取目标用户实例,再操作其文件夹:

const targetEmail = "department-shared@yourcompany.com";
const targetUser = GmailApp.getUserByName(targetEmail);

// 获取目标邮箱中名为"项目归档"的文件夹
const targetFolder = targetUser.getFolderByName("项目归档");

// 获取该文件夹下的所有会话
const folderThreads = targetFolder.getThreads();

关键注意点

  • 必须确保你的主账户拥有目标邮箱的合法访问权限,否则会触发权限错误。
  • 并非所有GmailApp方法都支持userId参数,部分全局配置类方法仅对当前主账户生效,可参考官方方法的参数说明确认。

内容的提问来源于stack exchange,提问作者Greg Truby

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 12:55:13