Stay organized with collections Save and categorize content based on your preferences.
There are two ways to send email using the Gmail API:
messages.send
method.drafts.send
method.Emails are sent as base64url encoded strings within the raw
property of a message resource. The high-level workflow to send an email is to:
raw
property to the base64url string you just created.messages.send
, or, if sending a draft, drafts.send
to send the message.The details of this workflow can vary depending on your choice of client library and programming language.
Creating messagesThe Gmail API requires MIME email messages compliant with RFC 2822 and encoded as base64url strings. Many programming languages have libraries or utilities that simplify the process of creating and encoding MIME messages. The following code examples demonstrate how to create a MIME message using the Google APIs client libraries for various languages.
JavaCreating an email message can be greatly simplified with the MimeMessage
class in the javax.mail.internet
package. The following example shows how to create the email message, including the headers:
The next step is to encode the MimeMessage
, instantiate a Message
object, and set the base64url encoded message string as the value of the raw
property.
The following code sample demonstrates creating a MIME message, encoding to a base64url string, and assigning it to the raw
field of the Message
resource:
Creating a message with an attachment is like creating any other message, but the process of uploading the file as a multi-part MIME message depends on the programming language. The following code examples demonstrate possible ways of creating a multi-part MIME message with an attachment.
JavaThe following example shows how to create a multi-part MIME message, the encoding and assignment steps are the same as above.
PythonSimilar to the previous example, this example also handles encoding the message to base64url and assigning it to the raw
field of the Message
resource.
Once you have created a message, you can send it by supplying it in the request body of a call to messages.send
, as demonstrated in the following examples.
If you're trying to send a reply and want the email to thread, make sure that:
Subject
headers matchReferences
and In-Reply-To
headers follow the RFC 2822 standard.For information on sending a message from a draft, see Creating Drafts.
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-04 UTC."],[],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4