Drafts represent unsent messages with the DRAFT
system label applied.
The message contained within the draft cannot be edited once created, but it
can be replaced. In this sense, the
draft resource is simply a container
that provides a stable ID because the underlying message IDs change every time
the message is replaced.
Message resources inside a draft have similar behavior to other messages except for the following differences:
- Draft messages cannot have any label other than the
DRAFT
system label. - When the draft is sent, the draft is automatically deleted and a new message
with an updated ID is created with the
SENT
system label. This message is returned in thedrafts.send
response.
Contents
Creating draft messages
Your application can create drafts using the drafts.create method. The general process is to:
- Create a MIME message that complies with RFC 2822.
- Convert the message to a base64url encoded string.
- Create a draft, setting the
value of the
drafts.message.raw
field to the encoded string.
The following code examples demonstrate the process.
Java
Creating 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
(download the example):
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.
Once you have a created a Message
, you can create a Draft
object and
pass it to the drafts.create
method.
Python
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 (download example):
Once you have created a Message
object, you can pass it to the
drafts.create
method to
create a Draft
object.
Updating drafts
Similarly to creating a draft, to update a draft you must supply a Draft
resource in the body of your request with the draft.message.raw
field
set to a base64url encoded string containing the MIME message. Because
messages cannot be updated, the message contained in the draft is destroyed
and replaced by the new MIME message supplied in the update request.
You can retrieve the current MIME message contained in the draft by calling
drafts.get
with the parameter
format=raw
.
For more information, see
drafts.update
.
Sending drafts
When sending a draft, you can choose to send the message as-is or as with an
updated message. If you are updating the draft content with a new message,
supply a Draft
resource in the body of the
drafts.send
request; set the
draft.id
of the draft to be sent; and set the draft.message.raw
field to the
new MIME message encoded as a base64url encoded string. For more
information, see drafts.send
.