# Gmail Service

Gmail

This Service lets you send email, compose drafts, manage labels, mark
messages and threads, and conduct a variety of other Gmail account management tasks.
See also [Mail Service](https://developers.google.com/apps-script/reference/mail.md.txt), a simpler service
that only allows the sending of email.

## Classes

| Name | Brief description |
|---|---|
| `GmailApp` | Provides access to Gmail threads, messages, and labels. |
| `GmailAttachment` | An attachment from Gmail. |
| `GmailDraft` | A user-created draft message in a user's Gmail account. |
| `GmailLabel` | A user-created label in a user's Gmail account. |
| `GmailMessage` | A message in a user's Gmail account. |
| `GmailThread` | A thread in a user's Gmail account. |

## `GmailApp`

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `createDraft(recipient, subject, body)` | `GmailDraft` | Creates a draft email message. |
| `createDraft(recipient, subject, body, options)` | `GmailDraft` | Creates a draft email message with optional arguments. |
| `createLabel(name)` | `GmailLabel` | Create a new user label of the given name. |
| `deleteLabel(label)` | `GmailApp` | Deletes the specified label. |
| `getAliases()` | `String[]` | Gets a list of the emails that are set up as aliases for this account in Gmail. |
| `getDraft(draftId)` | `GmailDraft` | Retrieve an email message draft by ID. |
| `getDraftMessages()` | `GmailMessage[]` | Retrieves all draft messages. |
| `getDrafts()` | `GmailDraft[]` | Gets all Gmail draft messages. |
| `getInboxThreads()` | `GmailThread[]` | Retrieves all Inbox threads irrespective of labels. |
| `getInboxThreads(start, max)` | `GmailThread[]` | Retrieves a range of Inbox threads irrespective of labels. |
| `getInboxUnreadCount()` | `Integer` | Gets the number of unread threads in the inbox. |
| `getMessageById(id)` | `GmailMessage` | Gets a message by ID. |
| `getMessagesForThread(thread)` | `GmailMessage[]` | Retrieve all messages in the specified thread. |
| `getMessagesForThreads(threads)` | `GmailMessage[][]` | Retrieve all messages in the specified threads. |
| `getPriorityInboxThreads()` | `GmailThread[]` | Retrieves all Priority Inbox threads irrespective of labels. |
| `getPriorityInboxThreads(start, max)` | `GmailThread[]` | Retrieves a range of Priority Inbox threads irrespective of labels. |
| `getPriorityInboxUnreadCount()` | `Integer` | Gets the number of unread threads in the Priority Inbox. |
| `getSpamThreads()` | `GmailThread[]` | Retrieves all spam threads irrespective of labels. |
| `getSpamThreads(start, max)` | `GmailThread[]` | Retrieves a range of spam threads irrespective of labels. |
| `getSpamUnreadCount()` | `Integer` | Gets the number of unread threads that are spam. |
| `getStarredThreads()` | `GmailThread[]` | Retrieves all starred threads irrespective of labels. |
| `getStarredThreads(start, max)` | `GmailThread[]` | Retrieves a range of starred threads irrespective of labels. |
| `getStarredUnreadCount()` | `Integer` | Gets the number of unread threads that are starred. |
| `getThreadById(id)` | `GmailThread|null` | Gets a thread by ID. |
| `getTrashThreads()` | `GmailThread[]` | Retrieves all trash threads irrespective of labels. |
| `getTrashThreads(start, max)` | `GmailThread[]` | Retrieves a range of trash threads irrespective of labels. |
| `getUserLabelByName(name)` | `GmailLabel` | Retrieves a label given the label name. |
| `getUserLabels()` | `GmailLabel[]` | Retrieves a list of user-created labels. |
| `markMessageRead(message)` | `GmailApp` | Marks this message read and forces the message to refresh. |
| `markMessageUnread(message)` | `GmailApp` | Marks this message unread and forces the message to refresh. |
| `markMessagesRead(messages)` | `GmailApp` | Marks these messages read and forces the messages to refresh. |
| `markMessagesUnread(messages)` | `GmailApp` | Marks these messages unread and forces the messages to refresh. |
| `markThreadImportant(thread)` | `GmailApp` | Marks this thread as important and forces the thread to refresh. |
| `markThreadRead(thread)` | `GmailApp` | Marks this thread as read and forces the thread to refresh. |
| `markThreadUnimportant(thread)` | `GmailApp` | Marks this thread as unimportant and forces the thread to refresh. |
| `markThreadUnread(thread)` | `GmailApp` | Marks this thread unread and forces the thread to refresh. |
| `markThreadsImportant(threads)` | `GmailApp` | Marks these threads as important and forces the threads to refresh. |
| `markThreadsRead(threads)` | `GmailApp` | Marks these threads as read and forces the threads to refresh. |
| `markThreadsUnimportant(threads)` | `GmailApp` | Marks these threads as unimportant and forces the threads to refresh. |
| `markThreadsUnread(threads)` | `GmailApp` | Marks these threads as unread and forces the threads to refresh. |
| `moveMessageToTrash(message)` | `GmailApp` | Moves the message to the trash and forces the message to refresh. |
| `moveMessagesToTrash(messages)` | `GmailApp` | Moves the specified messages to the trash and forces the messages to refresh. |
| `moveThreadToArchive(thread)` | `GmailApp` | Moves this thread to the archive and forces the thread to refresh. |
| `moveThreadToInbox(thread)` | `GmailApp` | Moves this thread to the inbox and forces the thread to refresh. |
| `moveThreadToSpam(thread)` | `GmailApp` | Moves this thread to spam and forces the thread to refresh. |
| `moveThreadToTrash(thread)` | `GmailApp` | Moves this thread to the trash and forces the thread to refresh. |
| `moveThreadsToArchive(threads)` | `GmailApp` | Moves these threads to the archive and forces the threads to refresh. |
| `moveThreadsToInbox(threads)` | `GmailApp` | Moves these threads to the inbox and forces the threads to refresh. |
| `moveThreadsToSpam(threads)` | `GmailApp` | Moves these threads to spam and forces the threads to refresh. |
| `moveThreadsToTrash(threads)` | `GmailApp` | Moves these threads to the trash and forces the threads to refresh. |
| `refreshMessage(message)` | `GmailApp` | Reloads the message and associated state from Gmail (useful in case the labels, read state, etc., have changed). |
| `refreshMessages(messages)` | `GmailApp` | Reloads the messages and associated state from Gmail (useful in case the labels, read state, etc., have changed). |
| `refreshThread(thread)` | `GmailApp` | Reloads the thread and associated state from Gmail (useful in case the labels, read state, etc., have changed). |
| `refreshThreads(threads)` | `GmailApp` | Reloads the threads and associated state from Gmail (useful in case the labels, read state, etc., have changed). |
| `search(query)` | `GmailThread[]` | Search Gmail with the given query. |
| `search(query, start, max)` | `GmailThread[]` | Search Gmail with the given query. |
| `sendEmail(recipient, subject, body)` | `GmailApp` | Sends an email message. |
| `sendEmail(recipient, subject, body, options)` | `GmailApp` | Sends an email message with optional arguments. |
| `setCurrentMessageAccessToken(accessToken)` | `void` | Sets the [current message access token](https://developers.google.com/workspace/add-ons/concepts/gsuite-scopes#access_tokens) that enables the script to access the current `GmailMessage` properties. |
| `starMessage(message)` | `GmailApp` | Adds a star to this message and forces the message to refresh. |
| `starMessages(messages)` | `GmailApp` | Adds stars to these messages and forces the messages to refresh. |
| `unstarMessage(message)` | `GmailApp` | Removes a star from this message and forces the message to refresh. |
| `unstarMessages(messages)` | `GmailApp` | Removes stars from these messages and forces the messages to refresh. |

## `GmailAttachment`

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `copyBlob()` | `Blob` | Returns a copy of this blob. |
| `getAs(contentType)` | `Blob` | Return the data inside this object as a blob converted to the specified content type. |
| `getBytes()` | `Byte[]` | Gets the data stored in this blob. |
| `getContentType()` | `String|null` | Gets the content type of the bytes in this blob. |
| `getDataAsString()` | `String` | Gets the data of this blob as a String with UTF-8 encoding. |
| `getDataAsString(charset)` | `String` | Gets the data of this blob as a string with the specified encoding. |
| `getHash()` | `String` | Gets the SHA1 content hash for this attachment. |
| `getName()` | `String|null` | Gets the name of this blob. |
| `getSize()` | `Integer` | Gets the size of this attachment. |
| `isGoogleType()` | `Boolean` | Returns whether this blob is a Google Workspace file (Sheets, Docs, etc.). |
| `setBytes(data)` | `Blob` | Sets the data stored in this blob. |
| `setContentType(contentType)` | `Blob` | Sets the content type of the bytes in this blob. |
| `setContentTypeFromExtension()` | `Blob` | Sets the content type of the bytes in this blob based on the file extension. |
| `setDataFromString(string)` | `Blob` | Sets the data of this blob from a string with UTF-8 encoding. |
| `setDataFromString(string, charset)` | `Blob` | Sets the data of this blob from a string with the specified encoding. |
| `setName(name)` | `Blob` | Sets the name of this blob. |

## `GmailDraft`

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `deleteDraft()` | `void` | Deletes this draft message. |
| `getId()` | `String` | Gets the ID of this draft message. |
| `getMessage()` | `GmailMessage` | Returns a GmailMessage representing this draft. |
| `getMessageId()` | `String` | Returns the ID of the `GmailMessage` representing this draft. |
| `send()` | `GmailMessage` | Sends this draft email message. |
| `update(recipient, subject, body)` | `GmailDraft` | Replaces the contents of this draft message. |
| `update(recipient, subject, body, options)` | `GmailDraft` | Replaces the contents of this draft message using optional arguments. |

## `GmailLabel`

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `addToThread(thread)` | `GmailLabel` | Adds this label to the given thread and forces the thread to refresh (`GmailThread.refresh()`). |
| `addToThreads(threads)` | `GmailLabel` | Adds this label to the given threads and forces the threads to refresh. |
| `deleteLabel()` | `void` | Deletes this label. |
| `getId()` | `String` | Gets the id of this label. |
| `getName()` | `String` | Gets the name of this label. |
| `getThreads()` | `GmailThread[]` | Gets the threads that are marked with this label. |
| `getThreads(start, max)` | `GmailThread[]` | Gets a range of threads marked with this label. |
| `getUnreadCount()` | `Integer` | Gets the number of unread threads tagged with this label. |
| `removeFromThread(thread)` | `GmailLabel` | Removes this label from the given thread and forces the thread to refresh. |
| `removeFromThreads(threads)` | `GmailLabel` | Removes this label from the given threads and forces the threads to refresh. |

## `GmailMessage`

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `createDraftReply(body)` | `GmailDraft` | Creates a draft message replying to the sender of this message using the reply-to address. |
| `createDraftReply(body, options)` | `GmailDraft` | Creates a draft message replying to the sender of this message using the reply-to address, with optional arguments. |
| `createDraftReplyAll(body)` | `GmailDraft` | Creates a draft message replying to the sender using the reply-to address and all recipients of this message. |
| `createDraftReplyAll(body, options)` | `GmailDraft` | Creates a draft message replying to the sender of this message using the reply-to address and all recipients, with optional arguments. |
| `forward(recipient)` | `GmailMessage` | Forwards this message to new recipients. |
| `forward(recipient, options)` | `GmailMessage` | Forwards this message to new recipients, with optional arguments. |
| `getAttachments()` | `GmailAttachment[]` | Gets all the attachments for this message. |
| `getAttachments(options)` | `GmailAttachment[]` | Gets all the attachments for this message. |
| `getBcc()` | `String` | Gets the comma-separated recipients bcc'd on this message. |
| `getBody()` | `String` | Gets the HTML content of the body of this message. |
| `getCc()` | `String` | Gets the comma-separated recipients cc'd on this message. |
| `getDate()` | `Date` | Gets the date and time of this message. |
| `getFrom()` | `String` | Gets the sender of this message. |
| `getHeader(name)` | `String` | Gets the value of an RFC 2822 header given the header name. |
| `getId()` | `String` | Gets the ID of this message. |
| `getPlainBody()` | `String` | Gets the content of the body of this message without HTML formatting. |
| `getRawContent()` | `String` | Gets the raw content of this message. |
| `getReplyTo()` | `String` | Gets the reply-to address of this message (usually the sender). |
| `getSubject()` | `String` | Gets the subject of this message. |
| `getThread()` | `GmailThread` | Gets the thread that contains this message. |
| `getTo()` | `String` | Gets the comma-separated recipients of this message. |
| `isDraft()` | `Boolean` | Gets whether this message is a draft. |
| `isInChats()` | `Boolean` | Gets whether this message is a chat. |
| `isInInbox()` | `Boolean` | Gets whether this message is in the inbox. |
| `isInPriorityInbox()` | `Boolean` | Returns `true` if this message is in the priority inbox; returns `false` otherwise. |
| `isInTrash()` | `Boolean` | Gets whether this message is in the trash. |
| `isStarred()` | `Boolean` | Gets whether this message is starred. |
| `isUnread()` | `Boolean` | Gets whether this message is unread. |
| `markRead()` | `GmailMessage` | Marks the message as read. |
| `markUnread()` | `GmailMessage` | Marks the message as unread. |
| `moveToTrash()` | `GmailMessage` | Moves the message to the trash. |
| `refresh()` | `GmailMessage` | Reloads this message and associated state from Gmail (useful in case the labels, read state, etc., have changed). |
| `reply(body)` | `GmailMessage` | Replies to the sender of this message using the reply-to address. |
| `reply(body, options)` | `GmailMessage` | Replies to the sender of this message using the reply-to address, with optional arguments. |
| `replyAll(body)` | `GmailMessage` | Replies to the sender using the reply-to address and all recipients of this message. |
| `replyAll(body, options)` | `GmailMessage` | Replies to the sender of this message using the reply-to address and all recipients, with optional arguments. |
| `star()` | `GmailMessage` | Stars the message. |
| `unstar()` | `GmailMessage` | Unstars the message. |

## `GmailThread`

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `addLabel(label)` | `GmailThread` | Adds this label to the thread. |
| `createDraftReply(body)` | `GmailDraft` | Creates a draft message replying to the sender of the last message in this thread using the reply-to address. |
| `createDraftReply(body, options)` | `GmailDraft` | Creates a draft message replying to the sender of the last message in this thread using the reply-to address, with optional arguments. |
| `createDraftReplyAll(body)` | `GmailDraft` | Creates a draft message replying to the sender of the last message in this thread, using the reply-to address and all recipients of this message. |
| `createDraftReplyAll(body, options)` | `GmailDraft` | Creates a draft message replying to the sender of the last message in this thread, using the reply-to address and all recipients, with optional arguments. |
| `getFirstMessageSubject()` | `String` | Gets the subject of the first message in the thread. |
| `getId()` | `String` | Gets the ID of this thread. |
| `getLabels()` | `GmailLabel[]` | Returns the user-created labels on this thread. |
| `getLastMessageDate()` | `Date` | Gets the date of this thread's most recent message. |
| `getMessageCount()` | `Integer` | Returns the number of messages in the thread. |
| `getMessages()` | `GmailMessage[]` | Gets the messages in this thread. |
| `getPermalink()` | `String` | Gets a permalink for this thread. |
| `hasStarredMessages()` | `Boolean` | Returns whether the thread has any starred messages. |
| `isImportant()` | `Boolean` | Returns whether the thread is marked as important. |
| `isInChats()` | `Boolean` | Returns whether the thread is labeled a chat. |
| `isInInbox()` | `Boolean` | Returns whether the thread is in the inbox. |
| `isInPriorityInbox()` | `Boolean` | Returns true if this thread is in the priority inbox; returns false otherwise. |
| `isInSpam()` | `Boolean` | Returns whether the thread is marked as spam. |
| `isInTrash()` | `Boolean` | Returns whether the thread is in the trash. |
| `isUnread()` | `Boolean` | Returns whether the thread has any unread messages. |
| `markImportant()` | `GmailThread` | Marks this thread as important. |
| `markRead()` | `GmailThread` | Marks this thread as read. |
| `markUnimportant()` | `GmailThread` | Marks this thread as unimportant. |
| `markUnread()` | `GmailThread` | Marks this thread as unread. |
| `moveToArchive()` | `GmailThread` | Moves this thread to the archive. |
| `moveToInbox()` | `GmailThread` | Moves this thread to the inbox. |
| `moveToSpam()` | `GmailThread` | Moves this thread to spam. |
| `moveToTrash()` | `GmailThread` | Moves this thread to the trash. |
| `refresh()` | `GmailThread` | Reloads this thread, and associated state from Gmail (useful in case the labels, read state, etc., have changed). |
| `removeLabel(label)` | `GmailThread` | Removes this label from the thread. |
| `reply(body)` | `GmailThread` | Reply to the sender of the last message on this thread using the replyTo address. |
| `reply(body, options)` | `GmailThread` | Reply to the sender of the last message on this thread using the replyTo address, with optional arguments. |
| `replyAll(body)` | `GmailThread` | Reply to the sender (using the replyTo address), and all recipients of the last message on this thread. |
| `replyAll(body, options)` | `GmailThread` | Reply to the sender (using the `replyTo` address), and all recipients of the last message on this thread, with optional arguments. |