Gmail Service

  • The Gmail service allows for a wide range of email and account management tasks beyond just sending emails.

  • This service includes classes for interacting with threads, messages, drafts, labels, and attachments within Gmail.

  • The GmailApp class provides methods for creating drafts and labels, deleting labels, retrieving various types of threads and messages, marking and moving messages and threads, and searching Gmail.

  • Additional classes such as GmailAttachment, GmailDraft, GmailLabel, GmailMessage, and GmailThread offer specific methods for managing their respective components.

  • The Mail Service is a simpler alternative for sending email only.

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, a simpler service that only allows the sending of email.

Classes

NameBrief 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

MethodReturn typeBrief 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() IntegerGets 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() IntegerGets 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() IntegerGets 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() IntegerGets the number of unread threads that are starred.
getThreadById(id) GmailThread 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) voidSets the current message access token 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

MethodReturn typeBrief 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() StringGets the content type of the bytes in this blob.
getDataAsString() StringGets the data of this blob as a String with UTF-8 encoding.
getDataAsString(charset) StringGets the data of this blob as a string with the specified encoding.
getHash() StringGets the SHA1 content hash for this attachment.
getName() StringGets the name of this blob.
getSize() IntegerGets the size of this attachment.
isGoogleType() BooleanReturns 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

MethodReturn typeBrief description
deleteDraft() voidDeletes this draft message.
getId() StringGets the ID of this draft message.
getMessage() GmailMessage Returns a GmailMessage representing this draft.
getMessageId() StringReturns 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

MethodReturn typeBrief 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() voidDeletes this label.
getId() StringGets the id of this label.
getName() StringGets 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() IntegerGets 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

MethodReturn typeBrief 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() StringGets the comma-separated recipients bcc'd on this message.
getBody() StringGets the HTML content of the body of this message.
getCc() StringGets the comma-separated recipients cc'd on this message.
getDate() DateGets the date and time of this message.
getFrom() StringGets the sender of this message.
getHeader(name) StringGets the value of an RFC 2822 header given the header name.
getId() StringGets the ID of this message.
getPlainBody() StringGets the content of the body of this message without HTML formatting.
getRawContent() StringGets the raw content of this message.
getReplyTo() StringGets the reply-to address of this message (usually the sender).
getSubject() StringGets the subject of this message.
getThread() GmailThread Gets the thread that contains this message.
getTo() StringGets the comma-separated recipients of this message.
isDraft() BooleanGets whether this message is a draft.
isInChats() BooleanGets whether this message is a chat.
isInInbox() BooleanGets whether this message is in the inbox.
isInPriorityInbox() BooleanReturns true if this message is in the priority inbox; returns false otherwise.
isInTrash() BooleanGets whether this message is in the trash.
isStarred() BooleanGets whether this message is starred.
isUnread() BooleanGets 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

MethodReturn typeBrief 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() StringGets the subject of the first message in the thread.
getId() StringGets the ID of this thread.
getLabels() GmailLabel[] Returns the user-created labels on this thread.
getLastMessageDate() DateGets the date of this thread's most recent message.
getMessageCount() IntegerReturns the number of messages in the thread.
getMessages() GmailMessage[] Gets the messages in this thread.
getPermalink() StringGets a permalink for this thread.
hasStarredMessages() BooleanReturns whether the thread has any starred messages.
isImportant() BooleanReturns whether the thread is marked as important.
isInChats() BooleanReturns whether the thread is labeled a chat.
isInInbox() BooleanReturns whether the thread is in the inbox.
isInPriorityInbox() BooleanReturns true if this thread is in the priority inbox; returns false otherwise.
isInSpam() BooleanReturns whether the thread is marked as spam.
isInTrash() BooleanReturns whether the thread is in the trash.
isUnread() BooleanReturns 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.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年08月04日 UTC.