提供对 Gmail 线程、邮件和标签的访问权限。
方法
详细文档
create Draft(recipient, subject, body)
创建电子邮件草稿。电子邮件的大小(包括标题)受配额限制。
// The code below creates a draft email with the current date and time. const now = new Date(); GmailApp.createDraft( 'mike@example.com', 'current time', `The time is: ${now.toString()}`, );
参数
| 名称 | 类型 | 说明 |
|---|---|---|
recipient | String | 以英文逗号分隔的电子邮件地址列表 |
subject | String | 电子邮件的主题 |
body | String | 电子邮件正文 |
返回
Gmail - 新创建的 GmailDraft
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
create Draft(recipient, subject, body, options)
创建一封电子邮件草稿,可使用可选实参。电子邮件可以包含纯文本或 HTML 正文。电子邮件的大小(包括标题,但不包括附件)受配额限制。
// Create a draft email with a file from Google Drive attached as a PDF. const file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz'); GmailApp.createDraft( 'mike@example.com', 'Attachment example', 'Please see attached file.', { attachments: [file.getAs(MimeType.PDF)], name: 'Automatic Emailer Script', }, );
参数
| 名称 | 类型 | 说明 |
|---|---|---|
recipient | String | 收件人的地址 |
subject | String | 主题行 |
body | String | 电子邮件的正文 |
options | Object | 一个 JavaScript 对象,用于指定高级参数,如下所列 |
高级参数
| 名称 | 类型 | 说明 |
|---|---|---|
attachments | Blob | 要随电子邮件一起发送的文件数组 |
bcc | String | 以英文逗号分隔的密送电子邮件地址列表 |
cc | String | 以逗号分隔的抄送电子邮件地址列表 |
from | String | 电子邮件的发送地址,必须是 get 返回的值之一 |
html | String | 如果设置了此参数,能够呈现 HTML 的设备将使用此参数,而不是必需的 body 实参;如果您为电子邮件添加了内嵌图片,则可以在 HTML 正文中添加可选的 inline 字段 |
inline | Object | 一个 JavaScript 对象,包含从图片键 (String) 到图片数据 (Blob) 的映射;这假设使用了 html 参数,并且该参数包含对这些图片的引用,格式为 <img src="cid:imageKey" /> |
name | String | 电子邮件发件人的名称(默认:用户的名称) |
reply | String | 用作默认回复地址的电子邮件地址 (默认值:用户的电子邮件地址) |
返回
Gmail - 新创建的 GmailDraft
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
create Label(name)
创建具有指定名称的新用户标签。
// Creates the label @FOO and logs label: FOO Logger.log(`label: ${GmailApp.createLabel('FOO')}`);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 新标签的名称 |
返回
Gmail - 新创建的标签
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
delete Label(label)
删除指定标签。
// Have to get the label by name first const label = GmailApp.getUserLabelByName('FOO'); GmailApp.deleteLabel(label);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
label | Gmail | 要删除的标签 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
get Aliases()
获取在 Gmail 中设置为相应账号的别名的电子邮件的列表。
您可以使用“from”可选实参通过上述任何别名发送消息。
// Log the aliases for this Gmail account and send an email as the first one. const me = Session.getActiveUser().getEmail(); const aliases = GmailApp.getAliases(); Logger.log(aliases); if (aliases.length > 0) { GmailApp.sendEmail(me, 'From an alias', 'A message from an alias!', { from: aliases[0], }); } else { GmailApp.sendEmail(me, 'No aliases found', 'You have no aliases.'); }
返回
String[] - 此账号的别名数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
get Draft(draftId)
按 ID 检索电子邮件草稿。
与 Gmail 草稿的 getId() 结合使用。
// Get the first draft message in your drafts folder const draft = GmailApp.getDrafts()[0]; // Get its ID const draftId = draft.getId(); // Now fetch the same draft using that ID. const draftById = GmailApp.getDraft(draftId); // Should always log true as they should be the same message Logger.log( draft.getMessage().getSubject() === draftById.getMessage().getSubject(), );
参数
| 名称 | 类型 | 说明 |
|---|---|---|
draft | String | 要检索的草稿的 ID |
返回
Gmail - 具有指定 ID 的草稿
抛出
Error - 如果找不到具有指定 ID 的草稿
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Draft Messages()
检索所有草稿邮件。
// Logs the number of draft messages const drafts = GmailApp.getDraftMessages(); Logger.log(drafts.length);
返回
Gmail - Gmail 邮件草稿的数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Drafts()
获取所有 Gmail 草稿邮件。
const drafts = GmailApp.getDrafts(); for (let i = 0; i < drafts.length; i++) { Logger.log(drafts[i].getId()); }
返回
Gmail - Gmail 邮件草稿的数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Inbox Threads()
检索所有收件箱会话,无论其是否带有标签。
如果所有线程的大小过大,导致系统无法处理,此调用将会失败。 如果线程大小未知且可能非常大,请使用“分页”调用,并在每次调用中指定要检索的线程范围。
// Log the subject lines of your Inbox const threads = GmailApp.getInboxThreads(); for (let i = 0; i < threads.length; i++) { Logger.log(threads[i].getFirstMessageSubject()); }
返回
Gmail - 收件箱中 Gmail 会话串的数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Inbox Threads(start, max)
检索一系列收件箱会话,而不考虑标签。
// Log the subject lines of up to the first 50 emails in your Inbox const threads = GmailApp.getInboxThreads(0, 50); for (let i = 0; i < threads.length; i++) { Logger.log(threads[i].getFirstMessageSubject()); }
参数
| 名称 | 类型 | 说明 |
|---|---|---|
start | Integer | 要检索的第一个线程的索引 |
max | Integer | 要检索的线程数量上限 |
返回
Gmail - 收件箱中 Gmail 会话串的数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Inbox Unread Count()
获取收件箱中未读消息串的数量。
Logger.log(`Messages unread in inbox: ${GmailApp.getInboxUnreadCount()}`);
返回
Integer - 收件箱中包含未读消息的会话数
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Message By Id(id)
根据 ID 获取消息。
与 Gmail 消息的 getId() 结合使用。
// Get the first message in the first thread of your inbox const message = GmailApp.getInboxThreads(0, 1)[0].getMessages()[0]; // Get its ID const messageId = message.getId(); // Now fetch the same message using that ID. const messageById = GmailApp.getMessageById(messageId); // Should always log true as they should be the same message Logger.log(message.getSubject() === messageById.getSubject());
参数
| 名称 | 类型 | 说明 |
|---|---|---|
id | String | 要检索的消息的 ID |
返回
Gmail - 具有指定 ID 的消息
抛出
Error - 如果找不到具有指定 ID 的消息
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
get Messages For Thread(thread)
检索指定会话串中的所有邮件。
// Log all the subject lines in the first thread of your inbox const thread = GmailApp.getInboxThreads(0, 1)[0]; const messages = GmailApp.getMessagesForThread(thread); for (let i = 0; i < messages.length; i++) { Logger.log(`subject: ${messages[i].getSubject()}`); }
参数
| 名称 | 类型 | 说明 |
|---|---|---|
thread | Gmail | 要检索的消息会话 |
返回
Gmail - 与相应线程对应的消息数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
get Messages For Threads(threads)
检索指定会话串中的所有邮件。
// Log the subject lines of all messages in the first two threads of your inbox const thread = GmailApp.getInboxThreads(0, 2); const messages = GmailApp.getMessagesForThreads(thread); for (let i = 0; i < messages.length; i++) { for (let j = 0; j < messages[i].length; j++) { Logger.log(`subject: ${messages[i][j].getSubject()}`); } }
参数
| 名称 | 类型 | 说明 |
|---|---|---|
threads | Gmail | 要检索的消息的线程 |
返回
Gmail - 消息数组的数组,其中外部数组中的每个项对应于一个会话,内部数组包含相应会话中的消息
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
get Priority Inbox Threads()
检索所有优先收件箱会话,无论其标签为何。
如果所有线程的大小过大,导致系统无法处理,此调用将会失败。 如果线程大小未知且可能非常大,请使用“分页”调用,并在每次调用中指定要检索的线程范围。
Logger.log( `# of messages in your Priority Inbox: ${ GmailApp.getPriorityInboxThreads().length}`, );
返回
Gmail - 优先收件箱中的 Gmail 会话串数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Priority Inbox Threads(start, max)
检索一系列优先收件箱会话,而不考虑标签。
// Will log some number 2 or less Logger.log( `# of messages in your Priority Inbox: ${ GmailApp.getPriorityInboxThreads(0, 2).length}`, );
参数
| 名称 | 类型 | 说明 |
|---|---|---|
start | Integer | 要检索的第一个线程的索引 |
max | Integer | 要检索的线程数量上限 |
返回
Gmail - 优先收件箱中的 Gmail 会话串数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Priority Inbox Unread Count()
获取优先收件箱中未读会话的数量。
Logger.log( `Number of unread emails in your Priority Inbox : ${ GmailApp.getPriorityInboxUnreadCount()}`, );
返回
Integer -“优先收件箱”中包含未读邮件的对话串数量
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Spam Threads()
检索所有垃圾内容线程,无论其标签如何。
如果所有线程的大小过大,导致系统无法处理,此调用将会失败。 如果线程大小未知且可能非常大,请使用“分页”调用,并在每次调用中指定要检索的线程范围。
Logger.log(`# of total spam threads: ${GmailApp.getSpamThreads().length}`);
返回
Gmail - 垃圾邮件文件夹中的 Gmail 会话串数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Spam Threads(start, max)
检索一系列垃圾邮件会话,而不考虑标签。
// Will log a number at most 5 Logger.log(`# of total spam threads: ${GmailApp.getSpamThreads(0, 5).length}`);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
start | Integer | 要检索的第一个线程的索引 |
max | Integer | 要检索的线程数量上限 |
返回
Gmail - 垃圾邮件文件夹中的 Gmail 会话串数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Spam Unread Count()
获取未读垃圾邮件会话的数量。
// Unless you actually read stuff in your spam folder, this should be the same // as the number of messages in your spam folder. Logger.log(`# unread threads that are spam: ${GmailApp.getSpamUnreadCount()}`);
返回
Integer - 包含未读消息的垃圾邮件对话串数量
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Starred Threads()
检索所有已加星标的会话,无论其标签如何。
如果所有线程的大小过大,导致系统无法处理,此调用将会失败。 如果线程大小未知且可能非常大,请使用“分页”调用,并在每次调用中指定要检索的线程范围。
// Logs the number of starred threads Logger.log(`# Starred threads: ${GmailApp.getStarredThreads().length}`);
返回
Gmail - 一个包含已加星标的 Gmail 会话的数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Starred Threads(start, max)
检索一系列已加星标的对话串,无论其是否带有标签。
// Logs the number of starred threads to a maximum of 5 Logger.log(`# Starred threads: ${GmailApp.getStarredThreads(0, 5).length}`);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
start | Integer | 要检索的第一个线程的索引 |
max | Integer | 要检索的线程数量上限 |
返回
Gmail - 一个包含已加星标的 Gmail 会话的数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Starred Unread Count()
获取已加星标的未读对话数量。
Logger.log(`# unread and starred: ${GmailApp.getStarredUnreadCount()}`);
返回
Integer - 已加星标且包含未读消息的会话数
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Thread By Id(id)
根据 ID 获取一个帖子。
与 Gmail 线程上的 getId() 结合使用。
// Gets the first inbox thread. const firstThread = GmailApp.getInboxThreads(0, 1)[0]; // Gets the same thread by ID. const threadById = GmailApp.getThreadById(firstThread.getId()); // Verifies that they are the same. console.log( firstThread.getFirstMessageSubject() === threadById.getFirstMessageSubject(), );
参数
| 名称 | 类型 | 说明 |
|---|---|---|
id | String | 要检索的线程的 ID。 |
返回
Gmail - 具有指定 ID 的线程,如果未找到,则为 null。
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
get Trash Threads()
检索所有已放入回收站的会话,无论其标签为何。
如果所有线程的大小过大,导致系统无法处理,此调用将会失败。 如果线程大小未知且可能非常大,请使用“分页”调用,并在每次调用中指定要检索的线程范围。
Logger.log(`# of total trash threads: ${GmailApp.getTrashThreads().length}`);
返回
Gmail - 垃圾箱中的 Gmail 会话串数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get Trash Threads(start, max)
检索一系列回收站中的线程,而不考虑标签。
// Will log a number at most 5 Logger.log( `# of total trash threads: ${GmailApp.getTrashThreads(0, 5).length}`, );
参数
| 名称 | 类型 | 说明 |
|---|---|---|
start | Integer | 要检索的第一个线程的索引 |
max | Integer | 要检索的线程数量上限 |
返回
Gmail - 垃圾箱中的 Gmail 会话串数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get User Label By Name(name)
根据标签名称检索标签。
const labelObject = GmailApp.getUserLabelByName('myLabel');
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 要检索的标签的名称 |
返回
Gmail - 具有指定名称的 Gmail 标签
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
get User Labels()
检索用户创建的标签的列表。
// Logs all of the names of your labels const labels = GmailApp.getUserLabels(); for (let i = 0; i < labels.length; i++) { Logger.log(`label: ${labels[i].getName()}`); }
返回
Gmail - 用户创建的标签数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
mark Message Read(message)
将相应消息标记为已读,并强制刷新消息。
// Mark the first message in the first thread of your inbox as read const message = GmailApp.getInboxThreads(0, 1)[0].getMessages()[0]; GmailApp.markMessageRead(message);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
message | Gmail | 要标记为已读的邮件 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Message Unread(message)
将相应消息标记为未读,并强制刷新该消息。
// Mark the first message in the first thread of your inbox as unread const message = GmailApp.getInboxThreads(0, 1)[0].getMessages()[0]; GmailApp.markMessageUnread(message);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
message | Gmail | 要标记为未读的消息 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Messages Read(messages)
将这些消息标记为已读,并强制刷新消息。
// Mark first three messages in the first inbox thread as read. // Assumes that the first inbox thread has 3 messages in it. const threadMessages = GmailApp.getInboxThreads(0, 1)[0].getMessages(); const messages = [threadMessages[0], threadMessages[1], threadMessages[2]]; GmailApp.markMessagesRead(messages);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
messages | Gmail | 要标记为已读的消息数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Messages Unread(messages)
将这些消息标记为未读,并强制刷新消息。
// Mark first three messages in the first inbox thread as unread. // Assumes that the first inbox thread has 3 messages in it const threadMessages = GmailApp.getInboxThreads(0, 1)[0].getMessages(); const messages = [threadMessages[0], threadMessages[1], threadMessages[2]]; GmailApp.markMessagesUnread(messages);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
messages | Gmail | 要标记为未读的消息数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Thread Important(thread)
将相应消息串标记为重要,并强制刷新该消息串。
// Marks first inbox thread as important const thread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.markThreadImportant(thread);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
thread | Gmail | 要标记为重要的消息串 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Thread Read(thread)
将相应消息串标记为已读,并强制刷新该消息串。
// Marks first inbox thread as read const thread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.markThreadRead(thread);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
thread | Gmail | 要标记为已读的消息串 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Thread Unimportant(thread)
将相应消息串标记为不重要,并强制刷新该消息串。
// Marks first inbox thread as unimportant const thread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.markThreadUnimportant(thread);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
thread | Gmail | 要标记为不重要的消息串 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Thread Unread(thread)
将相应消息串标记为未读,并强制刷新该消息串。
// Marks first inbox thread as unread const thread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.markThreadUnread(thread);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
thread | Gmail | 要标记为未读的消息串 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Threads Important(threads)
将这些线程标记为重要线程,并强制刷新这些线程。
// Marks first two threads in inbox as important const threads = GmailApp.getInboxThreads(0, 2); GmailApp.markThreadsImportant(threads);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
threads | Gmail | 要标记为重要的线程数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Threads Read(threads)
将这些消息串标记为已读,并强制刷新消息串。
// Marks first two threads in inbox as read const threads = GmailApp.getInboxThreads(0, 2); GmailApp.markThreadsRead(threads);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
threads | Gmail | 要标记为已读的消息串的数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Threads Unimportant(threads)
将这些消息串标记为不重要,并强制刷新消息串。
// Marks first two threads in inbox as unimportant const threads = GmailApp.getInboxThreads(0, 2); GmailApp.markThreadsUnimportant(threads);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
threads | Gmail | 要标记为不重要的线程的数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
mark Threads Unread(threads)
将这些消息串标记为未读,并强制刷新消息串。
// Marks first two threads in inbox as unread const threads = GmailApp.getInboxThreads(0, 2); GmailApp.markThreadsUnread(threads);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
threads | Gmail | 要标记为未读的消息串的数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
move Message To Trash(message)
将消息移至回收站并强制刷新消息。
// Move the first message in your inbox to trash const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const firstMessage = firstThread.getMessages()[0]; GmailApp.moveMessageToTrash(firstMessage);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
message | Gmail | 要移至回收站的邮件 |
返回
Gmail - Gmail 服务(适用于链式调用)
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
move Messages To Trash(messages)
将指定的消息移至回收站,并强制刷新消息。
// Move first two messages in your inbox to trash const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const messages = firstThread.getMessages(); const toDelete = [messages[0], messages[1]]; GmailApp.moveMessagesToTrash(toDelete);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
messages | Gmail | 要移至回收站的邮件 |
返回
Gmail - Gmail 服务(适用于链式调用)
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
move Thread To Archive(thread)
将相应线程移至归档,并强制刷新该线程。
// Archive the first thread in your inbox const firstThread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.moveThreadToArchive(firstThread);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
thread | Gmail | 要归档的会话串 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
move Thread To Inbox(thread)
将相应线程移至收件箱,并强制刷新该线程。
// Find a thread not already in your inbox const thread = GmailApp.search('-in:inbox')[0]; // Get the first one GmailApp.moveThreadToInbox(thread);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
thread | Gmail | 要移至收件箱的对话 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
move Thread To Spam(thread)
将相应对话串移至“垃圾内容”文件夹,并强制刷新该对话串。
// Tag first thread in inbox as spam const firstThread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.moveThreadToSpam(firstThread);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
thread | Gmail | 要移至“垃圾内容”的对话 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
move Thread To Trash(thread)
将相应线程移至回收站,并强制刷新该线程。
// Move first thread in inbox to trash const firstThread = GmailApp.getInboxThreads(0, 1)[0]; GmailApp.moveThreadToTrash(firstThread);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
thread | Gmail | 要放入回收站的会话串 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
move Threads To Archive(threads)
将这些线程移至归档文件夹,并强制刷新线程。
// Move first two threads in your inbox to the archive const firstTwoThreads = GmailApp.getInboxThreads(0, 2); GmailApp.moveThreadsToArchive(firstTwoThreads);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
threads | Gmail | 要归档的线程数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
move Threads To Inbox(threads)
将这些会话移至收件箱,并强制刷新会话。
// Find two threads not already in your inbox const firstTwoThreads = GmailApp.search('-in:inbox', 0, 2); GmailApp.moveThreadsToInbox(firstTwoThreads);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
threads | Gmail | 要移至收件箱的线程数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
move Threads To Spam(threads)
将这些会话移至“垃圾内容”文件夹,并强制刷新会话。
// Move first two threads in your inbox to spam const firstTwoThreads = GmailApp.getInboxThreads(0, 2); GmailApp.moveThreadsToSpam(firstTwoThreads);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
threads | Gmail | 要移至“垃圾内容”的线程数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
move Threads To Trash(threads)
将这些线程移至回收站,并强制刷新线程。
// Move first two threads in your inbox to trash const firstTwoThreads = GmailApp.getInboxThreads(0, 2); GmailApp.moveThreadsToTrash(firstTwoThreads);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
threads | Gmail | 要放入回收站的线程数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
refresh Message(message)
从 Gmail 重新加载消息和关联状态(如果标签、已读状态等发生变化,此方法非常有用)。
const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const firstMessage = firstThread.getMessages()[0]; // ...Do something that may take a while here.... GmailApp.refreshMessage(firstMessage); // ...Do more stuff with firstMessage...
参数
| 名称 | 类型 | 说明 |
|---|---|---|
message | Gmail | 要刷新的消息 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
refresh Messages(messages)
从 Gmail 重新加载消息和关联状态(如果标签、已读状态等发生变化,此方法非常有用)。
const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const coupleOfMessages = firstThread.getMessages().slice(0, 2); // ...Do something that may take a while here.... GmailApp.refreshMessages(coupleOfMessages); // ...Do more stuff with coupleOfMessages...
参数
| 名称 | 类型 | 说明 |
|---|---|---|
messages | Gmail | 要刷新的消息 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
refresh Thread(thread)
从 Gmail 重新加载会话和关联状态(如果标签、已读状态等发生变化,此方法非常有用)。
const firstThread = GmailApp.getInboxThreads(0, 1)[0]; // ...Do something that may take a while here.... GmailApp.refreshThread(firstThread); // ... Do more stuff with the thread ...
参数
| 名称 | 类型 | 说明 |
|---|---|---|
thread | Gmail | 要刷新的线程 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
refresh Threads(threads)
从 Gmail 重新加载线程和关联状态(如果标签、已读状态等发生更改,此方法非常有用)。
const threads = GmailApp.getInboxThreads(0, 3); // ...Do something that may take a while here.... GmailApp.refreshThreads(threads); // ... Do more stuff with threads ...
参数
| 名称 | 类型 | 说明 |
|---|---|---|
threads | Gmail | 要刷新的消息串 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
search(query)
使用指定查询内容搜索 Gmail。
如果所有线程的大小过大,导致系统无法处理,此调用将会失败。 如果线程大小未知且可能非常大,请使用“分页”调用,并在每次调用中指定要检索的线程范围。
// Find starred messages with subject IMPORTANT const threads = GmailApp.search('is:starred subject:"IMPORTANT"');
参数
| 名称 | 类型 | 说明 |
|---|---|---|
query | String | 搜索查询,就像您在 Gmail 中输入的那样 |
返回
Gmail - 与相应查询匹配的 Gmail 会话串数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
search(query, start, max)
使用指定查询内容搜索 Gmail。
// Find starred messages with subject IMPORTANT and return second batch of 10. // Assumes there are at least 11 of them, otherwise this will return an empty // array. const threads = GmailApp.search('is:starred subject:"IMPORTANT"', 10, 10);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
query | String | 搜索查询,就像您在 Gmail 中输入的那样 |
start | Integer | 起始线程的索引 |
max | Integer | 要返回的最大线程数 |
返回
Gmail - 与相应查询匹配的 Gmail 会话串数组
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
send Email(recipient, subject, body)
发送邮件。电子邮件的大小(包括标题)受配额限制。
// The code below will send an email with the current date and time. const now = new Date(); GmailApp.sendEmail( 'mike@example.com', 'current time', `The time is: ${now.toString()}`, );
参数
| 名称 | 类型 | 说明 |
|---|---|---|
recipient | String | 以英文逗号分隔的电子邮件地址列表 |
subject | String | 电子邮件的主题(最多 250 个字符) |
body | String | 电子邮件正文 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
send Email(recipient, subject, body, options)
发送包含可选实参的电子邮件。电子邮件可以包含纯文本或 HTML 正文。电子邮件的大小(包括标题,但不包括附件)受配额限制。
// Send an email with a file from Google Drive attached as a PDF. const file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz'); GmailApp.sendEmail( 'mike@example.com', 'Attachment example', 'Please see the attached file.', { attachments: [file.getAs(MimeType.PDF)], name: 'Automatic Emailer Script', }, );
参数
| 名称 | 类型 | 说明 |
|---|---|---|
recipient | String | 收件人的地址 |
subject | String | 主题行(最多 250 个字符) |
body | String | 电子邮件的正文 |
options | Object | 一个 JavaScript 对象,用于指定高级参数,如下所列 |
高级参数
| 名称 | 类型 | 说明 |
|---|---|---|
attachments | Blob | 要随电子邮件一起发送的文件数组 |
bcc | String | 以英文逗号分隔的密送电子邮件地址列表 |
cc | String | 以逗号分隔的抄送电子邮件地址列表 |
from | String | 电子邮件的发送地址,必须是 get 返回的值之一 |
html | String | 如果设置了此参数,能够呈现 HTML 的设备将使用此参数,而不是必需的 body 实参;如果电子邮件包含内嵌图片,您可以在 HTML 正文中添加可选的 inline 字段 |
inline | Object | 一个 JavaScript 对象,包含从图片键 (String) 到图片数据 (Blob) 的映射;这假设使用了 html 参数,并且该参数包含对这些图片的引用,格式为 <img src="cid:imageKey" /> |
name | String | 电子邮件发件人的名称(默认:用户的名称) |
no | Boolean | true 如果电子邮件应从通用的 no-reply 电子邮件地址发送,以阻止收件人回复电子邮件;此选项仅适用于 Google Workspace 账号,不适用于 Gmail 用户 |
reply | String | 用作默认回复地址的电子邮件地址 (默认值:用户的电子邮件地址) |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
set Current Message Access Token(accessToken)
设置当前消息访问令牌,使脚本能够访问当前的 Gmail 属性。
只有使用 Gmail 当前消息范围的 Google Workspace 加载项项目才需要使用此方法。
function handleAddonActionEvent(e) { GmailApp.setCurrentMessageAccessToken(e.messageMetadata.accessToken); const mailMessage = GmailApp.getMessageById(e.messageMetadata.messageId); // Do something with mailMessage }
参数
| 名称 | 类型 | 说明 |
|---|---|---|
access | String | 从 Gmail 插件操作事件对象获取的临时访问令牌。 |
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
star Message(message)
为相应消息添加星标,并强制刷新该消息。
// Stars the first message in the first thread in your inbox const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const message = firstThread.getMessages()[0]; GmailApp.starMessage(message);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
message | Gmail | 要加星标的邮件 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
star Messages(messages)
为这些消息添加星标,并强制刷新消息。
// Stars the first three messages in the first thread in your inbox const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const coupleOfMessages = firstThread.getMessages().slice(0, 3); GmailApp.starMessages(coupleOfMessages);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
messages | Gmail | 要加星标的消息的数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
unstar Message(message)
从相应消息中移除星标,并强制刷新该消息。
// Unstars the first message in the first thread in your inbox const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const message = firstThread.getMessages()[0]; GmailApp.unstarMessage(message);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
message | Gmail | 要移除星标的消息 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/
另请参阅
unstar Messages(messages)
移除这些消息中的星标,并强制刷新消息。
// Unstars the first three messages in the first thread in your inbox const firstThread = GmailApp.getInboxThreads(0, 1)[0]; const coupleOfMessages = firstThread.getMessages().slice(0, 3); GmailApp.unstarMessages(coupleOfMessages);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
messages | Gmail | 要移除星标的消息的数组 |
返回
Gmail - Gmail 服务,适用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围或相关 REST API 中的相应范围的授权:
-
https://mail.google.com/