Class GmailApp

Gmail應用程式

可存取 Gmail 討論串、郵件和標籤。

方法

方法傳回類型簡短說明
createDraft(recipient, subject, body)GmailDraft建立電子郵件草稿。
createDraft(recipient, subject, body, options)GmailDraft建立草擬電子郵件訊息,並可選擇性地提供引數。
createLabel(name)GmailLabel建立指定名稱的新使用者標籤。
deleteLabel(label)GmailApp刪除指定標籤。
getAliases()String[]取得 Gmail 中設為這個帳戶別名的電子郵件清單。
getDraft(draftId)GmailDraft依 ID 擷取電子郵件草稿。
getDraftMessages()GmailMessage[]擷取所有郵件草稿。
getDrafts()GmailDraft[]取得所有 Gmail 郵件草稿。
getInboxThreads()GmailThread[]擷取所有收件匣討論串,不論標籤為何。
getInboxThreads(start, max)GmailThread[]擷取一系列收件匣討論串,不論標籤為何。
getInboxUnreadCount()Integer取得收件匣中未讀討論串的數量。
getMessageById(id)GmailMessage依 ID 取得訊息。
getMessagesForThread(thread)GmailMessage[]擷取指定討論串中的所有訊息。
getMessagesForThreads(threads)GmailMessage[][]擷取指定討論串中的所有訊息。
getPriorityInboxThreads()GmailThread[]擷取所有優先收件匣討論串,不論標籤為何。
getPriorityInboxThreads(start, max)GmailThread[]擷取優先收件匣中的一系列郵件串,不論標籤為何。
getPriorityInboxUnreadCount()Integer取得優先收件匣中的未讀討論串數量。
getSpamThreads()GmailThread[]擷取所有垃圾內容討論串,不論標籤為何。
getSpamThreads(start, max)GmailThread[]擷取一系列垃圾內容討論串,不論標籤為何。
getSpamUnreadCount()Integer取得未讀垃圾郵件討論串的數量。
getStarredThreads()GmailThread[]擷取所有已加星號的討論串,不論標籤為何。
getStarredThreads(start, max)GmailThread[]擷取已加星號的郵件串範圍,不論標籤為何。
getStarredUnreadCount()Integer取得已加星號的未讀討論串數量。
getThreadById(id)GmailThread|null依 ID 取得執行緒。
getTrashThreads()GmailThread[]不論標籤為何,一律擷取所有垃圾桶中的郵件串。
getTrashThreads(start, max)GmailThread[]擷取一系列垃圾桶中的郵件串,不論標籤為何。
getUserLabelByName(name)GmailLabel根據標籤名稱擷取標籤。
getUserLabels()GmailLabel[]擷取使用者建立的標籤清單。
markMessageRead(message)GmailApp將這則訊息標示為已讀,並強制重新整理訊息。
markMessageUnread(message)GmailApp將這則訊息標示為未讀,並強制重新整理訊息。
markMessagesRead(messages)GmailApp將這些郵件標示為已讀取,並強制重新整理郵件。
markMessagesUnread(messages)GmailApp將這些郵件標示為未讀,並強制重新整理郵件。
markThreadImportant(thread)GmailApp將這個討論串標示為重要,並強制重新整理討論串。
markThreadRead(thread)GmailApp將這個討論串標示為已讀,並強制重新整理討論串。
markThreadUnimportant(thread)GmailApp將這個討論串標示為不重要,並強制重新整理討論串。
markThreadUnread(thread)GmailApp將這個討論串標示為未讀,並強制重新整理討論串。
markThreadsImportant(threads)GmailApp將這些討論串標示為重要,並強制重新整理討論串。
markThreadsRead(threads)GmailApp將這些討論串標示為已讀,並強制重新整理討論串。
markThreadsUnimportant(threads)GmailApp將這些討論串標示為不重要,並強制重新整理討論串。
markThreadsUnread(threads)GmailApp將這些討論串標示為未讀,並強制重新整理討論串。
moveMessageToTrash(message)GmailApp郵件會移至垃圾桶,並強制重新整理郵件。
moveMessagesToTrash(messages)GmailApp將指定郵件移至垃圾桶,並強制重新整理郵件。
moveThreadToArchive(thread)GmailApp將這個討論串移至封存,並強制重新整理討論串。
moveThreadToInbox(thread)GmailApp將這個討論串移至收件匣,並強制重新整理討論串。
moveThreadToSpam(thread)GmailApp將這個討論串移至垃圾訊息,並強制重新整理討論串。
moveThreadToTrash(thread)GmailApp將這個討論串移至垃圾桶,並強制重新整理討論串。
moveThreadsToArchive(threads)GmailApp將這些討論串移至封存,並強制重新整理討論串。
moveThreadsToInbox(threads)GmailApp將這些討論串移至收件匣,並強制重新整理討論串。
moveThreadsToSpam(threads)GmailApp將這些郵件串移至垃圾郵件,並強制重新整理郵件串。
moveThreadsToTrash(threads)GmailApp將這些討論串移至垃圾桶,並強制重新整理討論串。
refreshMessage(message)GmailApp從 Gmail 重新載入郵件和相關狀態 (如果標籤、已讀狀態等已變更,這項功能就很有用)。
refreshMessages(messages)GmailApp從 Gmail 重新載入郵件和相關狀態 (如果標籤、已讀狀態等已變更,這項功能就很有用)。
refreshThread(thread)GmailApp從 Gmail 重新載入郵件串和相關聯的狀態 (如果標籤、已讀狀態等已變更,這項功能就很有用)。
refreshThreads(threads)GmailApp從 Gmail 重新載入郵件串和相關狀態 (如果標籤、已讀狀態等已變更,這項功能就很有用)。
search(query)GmailThread[]使用指定查詢搜尋 Gmail。
search(query, start, max)GmailThread[]使用指定查詢搜尋 Gmail。
sendEmail(recipient, subject, body)GmailApp傳送電子郵件訊息。
sendEmail(recipient, subject, body, options)GmailApp傳送電子郵件,並可選擇是否加入引數。
setCurrentMessageAccessToken(accessToken)void設定目前訊息存取權杖,讓指令碼存取目前的 GmailMessage 屬性。
starMessage(message)GmailApp為郵件加上星號,並強制重新整理郵件。
starMessages(messages)GmailApp為這些郵件加上星號,並強制重新整理郵件。
unstarMessage(message)GmailApp移除郵件的星號,並強制重新整理郵件。
unstarMessages(messages)GmailApp移除這些郵件的星號,並強制重新整理郵件。

內容詳盡的說明文件

createDraft(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()}`,
);

參數

名稱類型說明
recipientString以半形逗號分隔的電子郵件地址清單
subjectString電子郵件主旨
bodyString電子郵件內文

回攻員

GmailDraft:新建立的 Gmail 草稿

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


createDraft(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',
    },
);

參數

名稱類型說明
recipientString收件者的地址
subjectString主旨行
bodyString電子郵件內文
optionsObject指定進階參數的 JavaScript 物件,如下所示

進階參數

名稱類型說明
attachmentsBlobSource[]要隨電子郵件傳送的檔案陣列
bccString以半形逗號分隔的密件副本電子郵件地址清單
ccString以半形逗號分隔的副本收件者電子郵件地址清單
fromString電子郵件的寄件者地址,必須是 getAliases() 傳回的值之一
htmlBodyString如果已設定,能夠算繪 HTML 的裝置會使用這個引數,而非必要的主體引數;如果電子郵件有內嵌圖片,您可以在 HTML 主體中加入選用的 inlineImages 欄位
inlineImagesObjectJavaScript 物件,內含圖片鍵 (String) 到圖片資料 (BlobSource) 的對應;這假設使用 htmlBody 參數,且該參數包含格式為 <img src="cid:imageKey" /> 的這些圖片的參照
nameString電子郵件寄件者的名稱 (預設為使用者名稱)
replyToString做為預設回覆地址的電子郵件地址 (預設值:使用者的電子郵件地址)

回攻員

GmailDraft:新建立的 Gmail 草稿

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


createLabel(name)

建立指定名稱的新使用者標籤。

// Creates the label @FOO and logs label: FOO
Logger.log(`label: ${GmailApp.createLabel('FOO')}`);

參數

名稱類型說明
nameString新標籤的名稱

回攻員

GmailLabel:新建立的標籤

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


deleteLabel(label)

刪除指定標籤。

// Have to get the label by name first
const label = GmailApp.getUserLabelByName('FOO');
GmailApp.deleteLabel(label);

參數

名稱類型說明
labelGmailLabel要刪除的標籤

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


getAliases()

取得 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/

另請參閱


getDraft(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(),
);

參數

名稱類型說明
draftIdString要擷取的草稿 ID

回攻員

GmailDraft - 具有指定 ID 的草稿

擲回

Error:如果找不到指定 ID 的草稿

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getDraftMessages()

擷取所有郵件草稿。

// Logs the number of draft messages
const drafts = GmailApp.getDraftMessages();
Logger.log(drafts.length);

回攻員

GmailMessage[]:Gmail 郵件草稿陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getDrafts()

取得所有 Gmail 郵件草稿。

const drafts = GmailApp.getDrafts();
for (let i = 0; i < drafts.length; i++) {
  Logger.log(drafts[i].getId());
}

回攻員

GmailDraft[] - Gmail 草稿郵件陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getInboxThreads()

擷取所有收件匣討論串,不論標籤為何。

如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。

// Log the subject lines of your Inbox
const threads = GmailApp.getInboxThreads();
for (let i = 0; i < threads.length; i++) {
  Logger.log(threads[i].getFirstMessageSubject());
}

回攻員

GmailThread[] - 收件匣中的 Gmail 討論串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getInboxThreads(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());
}

參數

名稱類型說明
startInteger要擷取的第一個執行緒的索引
maxInteger要擷取的執行緒數量上限

回攻員

GmailThread[] - 收件匣中的 Gmail 討論串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getInboxUnreadCount()

取得收件匣中未讀討論串的數量。

Logger.log(`Messages unread in inbox: ${GmailApp.getInboxUnreadCount()}`);

回攻員

Integer:收件匣中含有未讀郵件的討論串數量

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getMessageById(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());

參數

名稱類型說明
idString要擷取的訊息 ID

回攻員

GmailMessage - 具有指定 ID 的訊息

擲回

Error:如果找不到指定 ID 的訊息

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


getMessagesForThread(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()}`);
}

參數

名稱類型說明
threadGmailThread要擷取的訊息串

回攻員

GmailMessage[]:這個討論串對應的訊息陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


getMessagesForThreads(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()}`);
  }
}

參數

名稱類型說明
threadsGmailThread[]要擷取的訊息串

回攻員

GmailMessage[][]:訊息陣列的陣列,其中外部陣列中的每個項目都對應至一個討論串,而內部陣列則包含該討論串中的訊息

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


getPriorityInboxThreads()

不論標籤為何,都會擷取所有優先收件匣郵件串。

如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。

Logger.log(
    `# of messages in your Priority Inbox: ${
        GmailApp.getPriorityInboxThreads().length}`,
);

回攻員

GmailThread[] - 優先收件匣中的 Gmail 討論串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getPriorityInboxThreads(start, max)

擷取優先收件匣中的一系列郵件串,不論郵件串是否已加上標籤。

// Will log some number 2 or less
Logger.log(
    `# of messages in your Priority Inbox: ${
        GmailApp.getPriorityInboxThreads(0, 2).length}`,
);

參數

名稱類型說明
startInteger要擷取的第一個執行緒的索引
maxInteger要擷取的執行緒數量上限

回攻員

GmailThread[] - 優先收件匣中的 Gmail 討論串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getPriorityInboxUnreadCount()

取得優先收件匣中的未讀討論串數量。

Logger.log(
    `Number of unread emails in your Priority Inbox : ${
        GmailApp.getPriorityInboxUnreadCount()}`,
);

回攻員

Integer:優先收件匣中含有未讀郵件的郵件串數量

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getSpamThreads()

擷取所有垃圾內容討論串,不論標籤為何。

如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。

Logger.log(`# of total spam threads: ${GmailApp.getSpamThreads().length}`);

回攻員

GmailThread[]:垃圾郵件資料夾中的 Gmail 討論串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getSpamThreads(start, max)

不論標籤為何,都會擷取一系列垃圾郵件討論串。

// Will log a number at most 5
Logger.log(`# of total spam threads: ${GmailApp.getSpamThreads(0, 5).length}`);

參數

名稱類型說明
startInteger要擷取的第一個執行緒的索引
maxInteger要擷取的執行緒數量上限

回攻員

GmailThread[]:垃圾郵件資料夾中的 Gmail 討論串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getSpamUnreadCount()

取得垃圾郵件中未讀討論串的數量。

// 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/

getStarredThreads()

擷取所有已加星號的郵件串,不論標籤為何。

如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。

// Logs the number of starred threads
Logger.log(`# Starred threads: ${GmailApp.getStarredThreads().length}`);

回攻員

GmailThread[]:已加星號的 Gmail 郵件串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getStarredThreads(start, max)

擷取一系列已加星號的郵件串,不論標籤為何。

// Logs the number of starred threads to a maximum of 5
Logger.log(`# Starred threads: ${GmailApp.getStarredThreads(0, 5).length}`);

參數

名稱類型說明
startInteger要擷取的第一個執行緒的索引
maxInteger要擷取的執行緒數量上限

回攻員

GmailThread[]:已加星號的 Gmail 郵件串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getStarredUnreadCount()

取得已加星號的未讀討論串數量。

Logger.log(`# unread and starred: ${GmailApp.getStarredUnreadCount()}`);

回攻員

Integer:已加星號且有未讀訊息的討論串數量

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getThreadById(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(),
);

參數

名稱類型說明
idString要擷取的討論串 ID。

回攻員

GmailThread|null:具有指定 ID 的執行緒,或如果找不到,則為 null

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


getTrashThreads()

擷取所有垃圾桶中的郵件串,不論標籤為何。

如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。

Logger.log(`# of total trash threads: ${GmailApp.getTrashThreads().length}`);

回攻員

GmailThread[] - 垃圾桶中的 Gmail 討論串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getTrashThreads(start, max)

擷取一系列垃圾桶中的郵件串,不論標籤為何。

// Will log a number at most 5
Logger.log(
    `# of total trash threads: ${GmailApp.getTrashThreads(0, 5).length}`,
);

參數

名稱類型說明
startInteger要擷取的第一個執行緒的索引
maxInteger要擷取的執行緒數量上限

回攻員

GmailThread[] - 垃圾桶中的 Gmail 討論串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getUserLabelByName(name)

依標籤名稱擷取標籤。

const labelObject = GmailApp.getUserLabelByName('myLabel');

參數

名稱類型說明
nameString要擷取的標籤名稱

回攻員

GmailLabel:具有指定名稱的 Gmail 標籤

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

getUserLabels()

擷取使用者建立的標籤清單。

// 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()}`);
}

回攻員

GmailLabel[] - 使用者建立的標籤陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

markMessageRead(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);

參數

名稱類型說明
messageGmailMessage要標示為已讀的郵件

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markMessageUnread(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);

參數

名稱類型說明
messageGmailMessage要標示為未讀取的郵件

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markMessagesRead(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);

參數

名稱類型說明
messagesGmailMessage[]要標示為已讀的訊息陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markMessagesUnread(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);

參數

名稱類型說明
messagesGmailMessage[]要標示為未讀取的訊息陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markThreadImportant(thread)

將這個討論串標示為重要,並強制重新整理討論串。

// Marks first inbox thread as important
const thread = GmailApp.getInboxThreads(0, 1)[0];
GmailApp.markThreadImportant(thread);

參數

名稱類型說明
threadGmailThread要標示為重要的討論串

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markThreadRead(thread)

將這個討論串標示為已讀,並強制重新整理該討論串。

// Marks first inbox thread as read
const thread = GmailApp.getInboxThreads(0, 1)[0];
GmailApp.markThreadRead(thread);

參數

名稱類型說明
threadGmailThread要標示為已讀的討論串

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markThreadUnimportant(thread)

將這個討論串標示為不重要,並強制重新整理該討論串。

// Marks first inbox thread as unimportant
const thread = GmailApp.getInboxThreads(0, 1)[0];
GmailApp.markThreadUnimportant(thread);

參數

名稱類型說明
threadGmailThread要標示為不重要的討論串

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markThreadUnread(thread)

將這個討論串標示為未讀,並強制重新整理討論串。

// Marks first inbox thread as unread
const thread = GmailApp.getInboxThreads(0, 1)[0];
GmailApp.markThreadUnread(thread);

參數

名稱類型說明
threadGmailThread要標示為未讀的討論串

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markThreadsImportant(threads)

將這些討論串標示為重要,並強制重新整理討論串。

// Marks first two threads in inbox as important
const threads = GmailApp.getInboxThreads(0, 2);
GmailApp.markThreadsImportant(threads);

參數

名稱類型說明
threadsGmailThread[]要標示為重要的會話群組陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markThreadsRead(threads)

將這些討論串標示為已讀,並強制重新整理討論串。

// Marks first two threads in inbox as read
const threads = GmailApp.getInboxThreads(0, 2);
GmailApp.markThreadsRead(threads);

參數

名稱類型說明
threadsGmailThread[]要標示為已讀的討論串陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markThreadsUnimportant(threads)

將這些討論串標示為不重要,並強制重新整理討論串。

// Marks first two threads in inbox as unimportant
const threads = GmailApp.getInboxThreads(0, 2);
GmailApp.markThreadsUnimportant(threads);

參數

名稱類型說明
threadsGmailThread[]要標示為不重要的討論串陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


markThreadsUnread(threads)

將這些討論串標示為未讀,並強制重新整理討論串。

// Marks first two threads in inbox as unread
const threads = GmailApp.getInboxThreads(0, 2);
GmailApp.markThreadsUnread(threads);

參數

名稱類型說明
threadsGmailThread[]要標示為未讀的討論串陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


moveMessageToTrash(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);

參數

名稱類型說明
messageGmailMessage要刪除的郵件

回攻員

GmailApp - Gmail 服務 (適用於鏈結)

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


moveMessagesToTrash(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);

參數

名稱類型說明
messagesGmailMessage[]要刪除的郵件

回攻員

GmailApp - Gmail 服務 (適用於鏈結)

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


moveThreadToArchive(thread)

將這個討論串移至封存,並強制重新整理討論串。

// Archive the first thread in your inbox
const firstThread = GmailApp.getInboxThreads(0, 1)[0];
GmailApp.moveThreadToArchive(firstThread);

參數

名稱類型說明
threadGmailThread要封存的討論串

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


moveThreadToInbox(thread)

將這個討論串移至收件匣,並強制重新整理討論串。

// Find a thread not already in your inbox
const thread = GmailApp.search('-in:inbox')[0];  // Get the first one
GmailApp.moveThreadToInbox(thread);

參數

名稱類型說明
threadGmailThread要移至收件匣的討論串

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


moveThreadToSpam(thread)

將這個討論串移至垃圾訊息,並強制重新整理討論串。

// Tag first thread in inbox as spam
const firstThread = GmailApp.getInboxThreads(0, 1)[0];
GmailApp.moveThreadToSpam(firstThread);

參數

名稱類型說明
threadGmailThread要移至垃圾內容的討論串

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


moveThreadToTrash(thread)

將這個執行緒移至垃圾桶,並強制重新整理執行緒。

// Move first thread in inbox to trash
const firstThread = GmailApp.getInboxThreads(0, 1)[0];
GmailApp.moveThreadToTrash(firstThread);

參數

名稱類型說明
threadGmailThread要刪除的討論串

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


moveThreadsToArchive(threads)

將這些執行緒移至封存,並強制重新整理執行緒。

// Move first two threads in your inbox to the archive
const firstTwoThreads = GmailApp.getInboxThreads(0, 2);
GmailApp.moveThreadsToArchive(firstTwoThreads);

參數

名稱類型說明
threadsGmailThread[]要封存的討論串陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


moveThreadsToInbox(threads)

將這些郵件串移至收件匣,並強制重新整理郵件串。

// Find two threads not already in your inbox
const firstTwoThreads = GmailApp.search('-in:inbox', 0, 2);
GmailApp.moveThreadsToInbox(firstTwoThreads);

參數

名稱類型說明
threadsGmailThread[]要移至收件匣的執行緒陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


moveThreadsToSpam(threads)

將這些郵件串移至垃圾郵件,並強制重新整理郵件串。

// Move first two threads in your inbox to spam
const firstTwoThreads = GmailApp.getInboxThreads(0, 2);
GmailApp.moveThreadsToSpam(firstTwoThreads);

參數

名稱類型說明
threadsGmailThread[]要移至垃圾內容的執行緒陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


moveThreadsToTrash(threads)

將這些執行緒移至垃圾桶,並強制重新整理執行緒。

// Move first two threads in your inbox to trash
const firstTwoThreads = GmailApp.getInboxThreads(0, 2);
GmailApp.moveThreadsToTrash(firstTwoThreads);

參數

名稱類型說明
threadsGmailThread[]要刪除的執行緒陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


refreshMessage(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...

參數

名稱類型說明
messageGmailMessage要重新整理的訊息

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


refreshMessages(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...

參數

名稱類型說明
messagesGmailMessage[]要重新整理的訊息

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


refreshThread(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 ...

參數

名稱類型說明
threadGmailThread要重新整理的討論串

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


refreshThreads(threads)

從 Gmail 重新載入郵件串和相關聯的狀態 (如果標籤、已讀狀態等已變更,這項功能就很有用)。

const threads = GmailApp.getInboxThreads(0, 3);
// ...Do something that may take a while here....
GmailApp.refreshThreads(threads);
// ... Do more stuff with threads ...

參數

名稱類型說明
threadsGmailThread[]要重新整理的執行緒

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


search(query)

使用指定查詢搜尋 Gmail。

如果所有執行緒的大小過大,系統無法處理,這項呼叫就會失敗。 如果討論串大小不明,且可能非常大,請使用「分頁」呼叫,並在每次呼叫中指定要擷取的討論串範圍。

// Find starred messages with subject IMPORTANT
const threads = GmailApp.search('is:starred subject:"IMPORTANT"');

參數

名稱類型說明
queryString搜尋查詢,就像在 Gmail 中輸入一樣

回攻員

GmailThread[]:符合這項查詢的 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);

參數

名稱類型說明
queryString搜尋查詢,就像在 Gmail 中輸入一樣
startInteger起始執行緒的索引
maxInteger要傳回的執行緒數量上限

回攻員

GmailThread[]:符合這項查詢的 Gmail 討論串陣列

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

sendEmail(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()}`,
);

參數

名稱類型說明
recipientString以半形逗號分隔的電子郵件地址清單
subjectString電子郵件主旨 (最多 250 個半形字元)
bodyString電子郵件內文

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


sendEmail(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',
    },
);

參數

名稱類型說明
recipientString收件者的地址
subjectString主旨行 (最多 250 個半形字元)
bodyString電子郵件內文
optionsObject指定進階參數的 JavaScript 物件,如下所示

進階參數

名稱類型說明
attachmentsBlobSource[]要隨電子郵件傳送的檔案陣列
bccString以半形逗號分隔的密件副本電子郵件地址清單
ccString以半形逗號分隔的副本收件者電子郵件地址清單
fromString電子郵件的寄件者地址,必須是 getAliases() 傳回的值之一
htmlBodyString如果已設定,能夠算繪 HTML 的裝置會使用這個引數,而非必要的主體引數;如果電子郵件有內嵌圖片,您可以在 HTML 主體中加入選用的 inlineImages 欄位
inlineImagesObjectJavaScript 物件,內含圖片鍵 (String) 到圖片資料 (BlobSource) 的對應;這假設使用 htmlBody 參數,且該參數包含格式為 <img src="cid:imageKey" /> 的這些圖片的參照
nameString電子郵件寄件者的名稱 (預設為使用者名稱)
noReplyBooleantrue 如果電子郵件應從一般不回覆電子郵件地址傳送,以避免收件者回覆電子郵件;這個選項僅適用於 Google Workspace 帳戶,不適用於 Gmail 使用者
replyToString做為預設回覆地址的電子郵件地址 (預設值:使用者的電子郵件地址)

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


setCurrentMessageAccessToken(accessToken)

設定目前訊息存取權杖,讓指令碼存取目前的 GmailMessage 屬性。

只有使用 Gmail 目前郵件範圍Google Workspace 外掛程式專案才需要這個方法。

function handleAddonActionEvent(e) {
  GmailApp.setCurrentMessageAccessToken(e.messageMetadata.accessToken);
  const mailMessage = GmailApp.getMessageById(e.messageMetadata.messageId);
  // Do something with mailMessage
}

參數

名稱類型說明
accessTokenString從 Gmail 外掛程式動作事件物件取得的臨時存取權杖。

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

starMessage(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);

參數

名稱類型說明
messageGmailMessage要加上星號的郵件

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


starMessages(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);

參數

名稱類型說明
messagesGmailMessage[]要加上星號的訊息陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


unstarMessage(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);

參數

名稱類型說明
messageGmailMessage要移除星號的訊息

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱


unstarMessages(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);

參數

名稱類型說明
messagesGmailMessage[]要取消加星號的訊息陣列

回攻員

GmailApp:Gmail 服務,適用於串連

授權

使用這個方法的指令碼需要透過下列一或多個範圍相關 REST API 中的適當範圍授權:

  • https://mail.google.com/

另請參閱

已淘汰的方法