<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rfc SYSTEM 'rfc2629.dtd' []>
<rfc ipr="trust200902" category="std" docName="draft-ietf-jmap-mail-01" updates="5788">
<?rfc toc="yes"?>
<?rfc symrefs="yes"?>
<?rfc sortrefs="yes"?>
<?rfc compact="yes"?>
<?rfc subcompact="no"?>
<?rfc private=""?>
<?rfc topblock="yes"?>
<?rfc comments="no"?>
<front>
<title abbrev="JMAP Mail">JMAP for Mail</title>

<author initials="N.M." surname="Jenkins" fullname="Neil Jenkins">
<organization>FastMail</organization>
<address>
<postal>
<street>Level 1, 91 William St</street>
<city>Melbourne</city>
<code>VIC 3000</code>
<country>Australia</country>
<region></region>
</postal>
<phone></phone>
<email>neilj@fastmail.com</email>
<uri>https://www.fastmail.com</uri>
</address>
</author>
<date year="2017" month="July" day="16"/>

<area>Applications</area>
<workgroup>JMAP</workgroup>
<keyword>JMAP</keyword>
<keyword>JSON</keyword>
<keyword>email</keyword>


<abstract>
<t>This document specifies a data model for synchronising email data with a server using JMAP.
</t>
</abstract>


</front>

<middle>

<section anchor="introduction" title="Introduction">
<t>JMAP is a generic protocol for synchronising data, such as mail, calendars or contacts, between a client and a server. It is optimised for mobile and web environments, and aims to provide a consistent interface to different data types.
</t>
<t>This specification defines a data model for synchronising mail between a client and a server using JMAP.
</t>

<section anchor="notational-conventions" title="Notational Conventions">
<t>The key words &quot;MUST&quot;, &quot;MUST NOT&quot;, &quot;REQUIRED&quot;, &quot;SHALL&quot;, &quot;SHALL NOT&quot;, &quot;SHOULD&quot;, &quot;SHOULD NOT&quot;, &quot;RECOMMENDED&quot;, &quot;MAY&quot;, and &quot;OPTIONAL&quot; in this document are to be interpreted as described in <xref target="RFC2119"/>.
</t>
<t>The underlying format used for this specification is JSON. Consequently, the terms &quot;object&quot; and &quot;array&quot; as well as the four primitive types (strings, numbers, booleans, and null) are to be interpreted as described in Section 1 of <xref target="RFC7159"/>.
</t>
<t>Some examples in this document contain &quot;partial&quot; JSON documents used for illustrative purposes.  In these examples, three periods &quot;...&quot; are used to indicate a portion of the document that has been removed for compactness.
</t>
<t>Types signatures are given for all JSON objects in this document. The following conventions are used:
</t>
<t>
<list style="symbols">
<t><spanx style="verb">Boolean|String</spanx> – The value is either a JSON <spanx style="verb">Boolean</spanx> value, or a JSON <spanx style="verb">String</spanx> value.</t>
<t><spanx style="verb">Foo</spanx> – Any name that is not a native JSON type means an object for which the properties (and their types) are defined elsewhere within this document.</t>
<t><spanx style="verb">Foo[]</spanx> – An array of objects of type <spanx style="verb">Foo</spanx>.</t>
<t><spanx style="verb">String[Foo]</spanx> – A JSON <spanx style="verb">Object</spanx> being used as a map (associative array), where all the values are of type <spanx style="verb">Foo</spanx>.</t>
</list>
</t>
</section>

<section anchor="terminology" title="Terminology">
<t>The same terminology is used in this document as in the core JMAP specification.
</t>
</section>

<section anchor="addition-to-the-capabilities-object" title="Addition to the capabilities object">
<t>The capabilities object is returned as part of the standard JMAP authentication response; see the JMAP spec. Servers supporting <spanx style="emph">this</spanx> specification MUST add a property called <spanx style="verb">{TODO: URI for this spec}</spanx> to the capabilities object. The value of this property is an object which SHOULD contain the following information on server capabilities:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">maxSizeMessageAttachments</spanx>: <spanx style="verb">Number</spanx>
The maximum total size of attachments, in bytes, allowed for messages. A server MAY still reject messages with a lower attachment size total (for example, if the body includes several megabytes of text, causing the size of the encoded MIME structure to be over some server-defined limit).</t>
<t><spanx style="strong">canDelaySend</spanx>: <spanx style="verb">Boolean</spanx>
Does the server support inserting a message into the outbox to be sent later at a user-specified time?</t>
<t><spanx style="strong">messageListSortOptions</spanx>: <spanx style="verb">String[]</spanx>
A list of all the message properties the server supports for sorting by. This MAY include properties the client does not recognise (for example custom properties specified in a vendor extension). Clients MUST ignore any unknown properties in the list.</t>
</list>
</t>
</section>
</section>

<section anchor="mailboxes" title="Mailboxes">
<t>A mailbox represents a named set of emails. This is the primary mechanism for organising messages within an account. It is analogous to a folder or a label in other systems. A mailbox may perform a certain role in the system; see below for more details.
</t>
<t>For compatibility with IMAP, a message MUST belong to one or more mailboxes. The message id does not change if the message changes mailboxes.
</t>
<t>A <spanx style="strong">Mailbox</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">id</spanx>: <spanx style="verb">String</spanx>
The id of the mailbox. This property is immutable.</t>
<t><spanx style="strong">name</spanx>: <spanx style="verb">String</spanx>
User-visible name for the mailbox, e.g. &quot;Inbox&quot;. This may be any UTF-8  string (<xref target="RFC3629"/>) of at least 1 character in length and maximum 256 bytes in size. Servers SHOULD forbid sibling Mailboxes with the same name.</t>
<t><spanx style="strong">parentId</spanx>: <spanx style="verb">String|null</spanx>
The mailbox id for the parent of this mailbox, or <spanx style="verb">null</spanx> if this mailbox is at the top level. Mailboxes form acyclic graphs (forests) directed by the child-to-parent relationship. There MUST NOT be a loop.</t>
<t><spanx style="strong">role</spanx>: <spanx style="verb">String|null</spanx>
Identifies system mailboxes. This property can only be set on create. After the record has been created, this property is immutable.
The following values MUST be used for the relevant mailboxes:
<list style="symbols">
<t><spanx style="verb">inbox</spanx> – the mailbox to which new mail is delivered by default, unless diverted by a rule or spam filter etc.</t>
<t><spanx style="verb">archive</spanx> – messages the user does not need right now, but does not wish to delete.</t>
<t><spanx style="verb">drafts</spanx> – messages the user is currently writing and are not yet sent.</t>
<t><spanx style="verb">outbox</spanx> – messages the user has finished writing and wishes to send (see the <spanx style="verb">setMessages</spanx> method description for more information). A mailbox with this role MUST be present if the user is allowed to send mail through an account. If not present, the user cannot send mail with that account.</t>
<t><spanx style="verb">sent</spanx> – messages the user has sent.</t>
<t><spanx style="verb">trash</spanx> – messages the user has deleted.</t>
<t><spanx style="verb">spam</spanx> – messages considered spam by the server.</t>
<t><spanx style="verb">templates</spanx> – drafts which should be used as templates (i.e. used as the basis for creating new drafts).</t>
</list>
No two mailboxes may have the same role. Mailboxes without a known purpose MUST have a role of <spanx style="verb">null</spanx>.
An account is not required to have mailboxes with any of the above roles. A client MAY create new mailboxes with a role property to help them keep track of a use-case not covered by the above list. To avoid potential conflict with any special behaviour a server might apply to mailboxes with certain roles in the future, any roles not in the above list created by the client must begin with <spanx style="verb">"x-"</spanx>. The client MAY attempt to create mailboxes with the standard roles if not already present, but the server MAY reject these.</t>
<t><spanx style="strong">sortOrder</spanx>: <spanx style="verb">Number</spanx>
Defines the sort order of mailboxes when presented in the client's UI, so it
is consistent between devices. The number MUST be an integer in the range
0 &lt;= sortOrder &lt; 2^31.
A mailbox with a lower order should be displayed before a mailbox with a higher order (that has the same parent) in any mailbox listing in the client's UI. Mailboxes with equal order SHOULD be sorted in alphabetical order by name. The sorting SHOULD take into account locale-specific character order convention.</t>
<t><spanx style="strong">mustBeOnlyMailbox</spanx>: <spanx style="verb">Boolean</spanx>
If <spanx style="verb">true</spanx>, messages in this mailbox may not also be in any other mailbox that also has <spanx style="verb">mustBeOnlyMailbox: true</spanx>. If <spanx style="verb">false</spanx>, messages may be added to this mailbox in addition to any other mailbox (i.e. it's a label). This property is immutable.</t>
<t><spanx style="strong">mayReadItems</spanx>: <spanx style="verb">Boolean</spanx>
If true, may use this mailbox as part of a filter in a <spanx style="emph">getMessageList</spanx> call.
If a submailbox is shared but not the parent mailbox, this may be <spanx style="verb">false</spanx>.</t>
<t><spanx style="strong">mayAddItems</spanx>: <spanx style="verb">Boolean</spanx>
The user may add messages to this mailbox (by either creating a new message or moving an existing one).</t>
<t><spanx style="strong">mayRemoveItems</spanx>: <spanx style="verb">Boolean</spanx>
The user may remove messages from this mailbox (by either changing the mailboxes of a message or deleting it).</t>
<t><spanx style="strong">mayCreateChild</spanx>: <spanx style="verb">Boolean</spanx>
The user may create a mailbox with this mailbox as its parent.</t>
<t><spanx style="strong">mayRename</spanx>: <spanx style="verb">Boolean</spanx>
The user may rename the mailbox or make it a child of another mailbox.</t>
<t><spanx style="strong">mayDelete</spanx>: <spanx style="verb">Boolean</spanx>
The user may delete the mailbox itself.</t>
<t><spanx style="strong">totalMessages</spanx>: <spanx style="verb">Number</spanx>
The number of messages in this mailbox.</t>
<t><spanx style="strong">unreadMessages</spanx>: <spanx style="verb">Number</spanx>
The number of messages in this mailbox that have neither the <spanx style="verb">$Seen</spanx> keyword nor the <spanx style="verb">$Draft</spanx> keyword.</t>
<t><spanx style="strong">totalThreads</spanx>: <spanx style="verb">Number</spanx>
The number of threads where at least one message in the thread is in this mailbox.</t>
<t><spanx style="strong">unreadThreads</spanx>: <spanx style="verb">Number</spanx>
The number of threads where at least one message in the thread has neither the <spanx style="verb">$Seen</spanx> keyword nor the <spanx style="verb">$Draft</spanx> keyword AND at least one message in the thread is in this mailbox (but see below for special case handling of Trash). Note, the unread message does not need to be the one in this mailbox.</t>
</list>
</t>
<t>The Trash mailbox (that is a mailbox with <spanx style="verb">role == "trash"</spanx>) MUST be treated specially for the purpose of unread counts:
</t>
<t>
<list style="numbers">
<t>Messages that are <spanx style="strong">only</spanx> in the Trash (and no other mailbox) are ignored when calculating the <spanx style="verb">unreadThreads</spanx> count of other mailboxes.</t>
<t>Messages that are <spanx style="strong">not</spanx> in the Trash are ignored when calculating the <spanx style="verb">unreadThreads</spanx> count for the Trash mailbox.</t>
</list>
</t>
<t>The result of this is that messages in the Trash are treated as though they are in a separate thread for the purposes of unread counts. It is expected that clients will hide messages in the Trash when viewing a thread in another mailbox and vice versa. This allows you to delete a single message to the Trash out of a thread.
</t>
<t>So for example, suppose you have an account where the entire contents is a single conversation with 2 messages: an unread message in the Trash and a read message in the Inbox. The <spanx style="verb">unreadThreads</spanx> count would be <spanx style="verb">1</spanx> for the Trash and <spanx style="verb">0</spanx> for the Inbox.
</t>
<t>For IMAP compatibility, a message in both the Trash and another mailbox SHOULD be treated by the client as existing in both places (i.e. when emptying the trash, the client SHOULD just remove the Trash mailbox and leave it in the other mailbox).
</t>

<section anchor="getmailboxes" title="getMailboxes">
<t>Mailboxes can either be fetched explicitly by id, or all of them at once. To fetch mailboxes, make a call to <spanx style="verb">getMailboxes</spanx>. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The Account to fetch the mailboxes for. If <spanx style="verb">null</spanx>, the primary account is used.</t>
<t><spanx style="strong">ids</spanx>: <spanx style="verb">String[]|null</spanx>
The ids of the mailboxes to fetch. If <spanx style="verb">null</spanx>, all mailboxes in the account are returned.</t>
<t><spanx style="strong">properties</spanx>: <spanx style="verb">String[]|null</spanx>
The properties of each mailbox to fetch. If <spanx style="verb">null</spanx>, all properties are returned. The id of the mailbox will <spanx style="strong">always</spanx> be returned, even if not explicitly requested.</t>
</list>
</t>
<t>The response to <spanx style="emph">getMailboxes</spanx> is called <spanx style="emph">mailboxes</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">state</spanx>: <spanx style="verb">String</spanx>
A string representing the state on the server for <spanx style="strong">all</spanx> mailboxes. If the state of a mailbox changes, or a new mailbox is created, or a mailbox is destroyed, this string will change. It is used to get delta updates.</t>
<t><spanx style="strong">list</spanx>: <spanx style="verb">Mailbox[]</spanx>
An array of the Mailbox objects requested. This will be the <spanx style="strong">empty array</spanx> if the <spanx style="emph">ids</spanx> argument was the empty array, or contained only ids for mailboxes that could not be found.</t>
<t><spanx style="strong">notFound</spanx>: <spanx style="verb">String[]|null</spanx>
This array contains the ids passed to the method for mailboxes that do not exist, or <spanx style="verb">null</spanx> if all requested ids were found. It MUST be <spanx style="verb">null</spanx> if the <spanx style="emph">ids</spanx> argument in the call was <spanx style="verb">null</spanx>.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="emph">mailboxes</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">requestTooLarge</spanx>: Returned if the number of <spanx style="emph">ids</spanx> requested by the client exceeds the maximum number the server is willing to process in a single method call.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if the request does not include one of the required arguments, or one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="verb">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
</section>

<section anchor="getmailboxupdates" title="getMailboxUpdates">
<t>The <spanx style="emph">getMailboxUpdates</spanx> call allows a client to efficiently update the state of its cached mailboxes to match the new state on the server. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If <spanx style="verb">null</spanx>, the primary account will be used.</t>
<t><spanx style="strong">sinceState</spanx>: <spanx style="verb">String</spanx>
The current state of the client. This is the string that was returned as the <spanx style="emph">state</spanx> argument in the <spanx style="emph">mailboxes</spanx> response. The server will return the changes made since this state.</t>
<t><spanx style="strong">fetchRecords</spanx>: <spanx style="verb">Boolean|null</spanx>
If <spanx style="verb">true</spanx>, immediately after outputting a <spanx style="emph">mailboxUpdates</spanx> response, an implicit call will be made to <spanx style="emph">getMailboxes</spanx> with the <spanx style="emph">changed</spanx> property of the response as the <spanx style="emph">ids</spanx> argument, and the <spanx style="emph">fetchRecordProperties</spanx> argument as the <spanx style="emph">properties</spanx> argument. If <spanx style="verb">false</spanx> or <spanx style="verb">null</spanx>, no implicit call will be made.</t>
<t><spanx style="strong">fetchRecordProperties</spanx>: <spanx style="verb">String[]|null</spanx>
If <spanx style="verb">null</spanx>, all Mailbox properties will be fetched unless <spanx style="emph">onlyCountsChanged</spanx> in the <spanx style="emph">mailboxUpdates</spanx> response is <spanx style="verb">true</spanx>, in which case only the 4 counts properties will be returned (<spanx style="emph">totalMessages</spanx>, <spanx style="emph">unreadMessages</spanx>, <spanx style="emph">totalThreads</spanx> and <spanx style="emph">unreadThreads</spanx>). If not <spanx style="verb">null</spanx>, this value will be passed through to the <spanx style="emph">getMailboxes</spanx> call regardless of the <spanx style="emph">onlyCountsChanged</spanx> value in the <spanx style="emph">mailboxUpdates</spanx> response.</t>
</list>
</t>
<t>The response to <spanx style="emph">getMailboxUpdates</spanx> is called <spanx style="emph">mailboxUpdates</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String</spanx>
This is the <spanx style="emph">sinceState</spanx> argument echoed back; the state from which the server is returning changes.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
This is the state the client will be in after applying the set of changes to the old state.</t>
<t><spanx style="strong">changed</spanx>: <spanx style="verb">String[]</spanx>
An array of Mailbox ids where a property of the mailbox has changed between the old state and the new state, or the mailbox has been created, and the mailbox has not been destroyed.</t>
<t><spanx style="strong">removed</spanx>: <spanx style="verb">String[]</spanx>
An array of Mailbox ids for mailboxes which have been destroyed since the old state.</t>
<t><spanx style="strong">onlyCountsChanged</spanx>: <spanx style="verb">Boolean</spanx>
Indicates that only the mailbox counts (unread/total messages/threads) have changed since the old state. The client can then use this to optimise its data transfer and only fetch the counts. If the server is unable to tell if only counts have changed, it should just always return <spanx style="verb">false</spanx>.</t>
</list>
</t>
<t>If a mailbox has been modified AND deleted since the oldState, the server should just return the id in the <spanx style="emph">removed</spanx> array, but MAY return it in the <spanx style="emph">changed</spanx> array as well. If a mailbox has been created AND deleted since the oldState, the server SHOULD remove the mailbox id from the response entirely, but MAY include it in the <spanx style="emph">removed</spanx> array.
</t>
<t>The following errors may be returned instead of the <spanx style="verb">mailboxUpdates</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if the request does not include one of the required arguments, or one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="verb">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t><spanx style="verb">cannotCalculateChanges</spanx>: Returned if the server cannot calculate the changes from the state string given by the client. Usually due to the client's state being too old. The client MUST invalidate its Mailbox cache.
</t>
</section>

<section anchor="setmailboxes" title="setMailboxes">
<t>Mailboxes can be created, updated and destroyed using the <spanx style="emph">setMailboxes</spanx> method. The method takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If <spanx style="verb">null</spanx>, defaults to the primary account.</t>
<t><spanx style="strong">ifInState</spanx>: <spanx style="verb">String|null</spanx>
This is a state string as returned by the <spanx style="emph">getMailboxes</spanx> method. If supplied, the string must match the current state, otherwise the method will be aborted and a <spanx style="verb">stateMismatch</spanx> error returned. If <spanx style="verb">null</spanx>, any changes will be applied to the current state.</t>
<t><spanx style="strong">create</spanx>: <spanx style="verb">String[Mailbox]|null</spanx>
A map of <spanx style="emph">creation id</spanx> (an arbitrary string set by the client) to Mailbox objects. If <spanx style="verb">null</spanx>, no objects will be created.</t>
<t><spanx style="strong">update</spanx>: <spanx style="verb">String[Mailbox]|null</spanx>
A map of mailbox id to objects containing the properties to update for that Mailbox. If <spanx style="verb">null</spanx>, no objects will be updated.</t>
<t><spanx style="strong">destroy</spanx>: <spanx style="verb">String[]|null</spanx>
A list of ids for Mailboxes to permanently delete. If <spanx style="verb">null</spanx>, no objects will be deleted.</t>
</list>
</t>
<t>If a create, update or destroy is rejected, the appropriate error MUST be added to the notCreated/notUpdated/notDestroyed property of the response and the server MUST continue to the next create/update/destroy. It does not terminate the method.
</t>

<section anchor="ordering-of-changes" title="Ordering of changes">
<t>Each create, update or destroy is considered an atomic unit. The server MAY commit some of the changes but not others, however MUST NOT only commit part of an update to a single record (e.g. update the <spanx style="emph">name</spanx> field but not the <spanx style="emph">parentId</spanx> field, if both are supplied in the update object).
</t>
<t>The final state MUST be valid after the setMailboxes is finished, however the server MAY have to transition through invalid intermediate states (not exposed to the client) while processing the individual create/update/destroy requests. For example, a single method call could rename Mailbox A =&gt; B, and simultaneously rename Mailbox B =&gt; A. The final state is valid, so this is allowed, however if processed sequentially there will be an internal state where temporarily both mailboxes have the same name.
</t>
<t>A Mailbox may reference another Mailbox object as a parent. When a Mailbox is created or modified, it may reference another Mailbox being created <spanx style="emph">in the same API request</spanx> by using the creation id prefixed with a <spanx style="verb">#</spanx>. The order of the method calls in the request by the client MUST be such that the mailbox being referenced is created in <spanx style="strong">either the same or an earlier method call</spanx>. If within the same method call, the server MUST process the parent create first, as if this fails the create/update that references it will also fail.
</t>
<t>Creation ids sent by the client SHOULD be unique within the single API request for a particular data type. If a creation id is reused, the server MUST map the creation id to the most recently created item with that id.
</t>
</section>

<section anchor="creating-mailboxes" title="Creating mailboxes">
<t>The properties of the Mailbox object submitted for creation MUST conform to the following conditions:
</t>
<t>
<list style="symbols">
<t>The <spanx style="emph">id</spanx> property MUST NOT be present.</t>
<t>The <spanx style="emph">parentId</spanx> property MUST be either <spanx style="verb">null</spanx> or be a valid id for a mailbox for which the <spanx style="verb">mayCreateChild</spanx> property is <spanx style="verb">true</spanx>.</t>
<t>The <spanx style="emph">role</spanx> property MUST be either <spanx style="verb">null</spanx>, a valid role as listed in the Mailbox object specification, or prefixed by <spanx style="verb">"x-"</spanx>.</t>
<t>The <spanx style="emph">mustBeOnlyMailbox</spanx> property MUST NOT be present. This is server dependent and will be set by the server.</t>
<t>The <spanx style="emph">mayXXX</spanx> properties MUST NOT be present. Restrictions may only be set by the server for system mailboxes, or when sharing mailboxes with other users (setting sharing is not defined yet in this spec).</t>
<t>The <spanx style="emph">totalMessages</spanx>, <spanx style="emph">unreadMessages</spanx>, <spanx style="emph">totalThreads</spanx> and <spanx style="emph">unreadThreads</spanx> properties MUST NOT be present.</t>
</list>
</t>
<t>If any of the properties are invalid, the server MUST reject the create with an <spanx style="verb">invalidProperties</spanx> error. The Error object SHOULD contain a property called <spanx style="emph">properties</spanx> of type <spanx style="verb">String[]</spanx> that lists <spanx style="strong">all</spanx> the properties that were invalid. The object MAY also contain a <spanx style="emph">description</spanx> property of type <spanx style="verb">String</spanx> with a user-friendly description of the problems.
</t>
<t>There may be a maximum number of mailboxes allowed on the server. If this is reached, any attempt at creation will be rejected with a <spanx style="verb">maxQuotaReached</spanx> error.
</t>
</section>

<section anchor="updating-mailboxes" title="Updating mailboxes">
<t>If the <spanx style="emph">id</spanx> given does not correspond to a Mailbox in the given account, the update MUST be rejected with a <spanx style="verb">notFound</spanx> error.
</t>
<t>All properties being updated must be of the correct type, not immutable or server-set-only, and the new value must obey all conditions of the property. In particular, note the following conditions:
</t>
<t>
<list style="symbols">
<t>The <spanx style="emph">name</spanx> property MUST be a valid UTF-8 string of at least 1 character in length and maximum 256 bytes in size.</t>
<t>The <spanx style="emph">parentId</spanx> property MUST be either <spanx style="verb">null</spanx> or be a valid id for <spanx style="emph">another</spanx> mailbox that is <spanx style="strong">not a descendant</spanx> of this mailbox, and for which the <spanx style="verb">mayCreateChild</spanx> property is <spanx style="verb">true</spanx>.</t>
<t>These properties are immutable or may only be set by the server:
<list style="symbols">
<t>id</t>
<t>role</t>
<t>mustBeOnlyMailbox</t>
<t>mayReadItems</t>
<t>mayAddItems</t>
<t>mayRemoveItems</t>
<t>mayCreateChild</t>
<t>mayRename</t>
<t>mayDelete</t>
<t>totalMessages</t>
<t>unreadMessages</t>
<t>totalThreads</t>
<t>unreadThreads</t>
</list></t>
</list>
</t>
<t>If any of the properties are invalid, the server MUST reject the update with an <spanx style="verb">invalidProperties</spanx> error. The Error object SHOULD contain a property called <spanx style="emph">properties</spanx> of type <spanx style="verb">String[]</spanx> that lists <spanx style="strong">all</spanx> the properties that were invalid. The object MAY also contain a <spanx style="emph">description</spanx> property of type <spanx style="verb">String</spanx> with a user-friendly description of the problems.
</t>
</section>

<section anchor="destroying-mailboxes" title="Destroying mailboxes">
<t>If the <spanx style="emph">id</spanx> given does not correspond to a Mailbox in the given account, the destruction MUST be rejected with a <spanx style="verb">notFound</spanx> error.
</t>
<t>If the mailbox has <spanx style="verb">mayDeleteMailbox == false</spanx>, the destruction MUST be rejected with a <spanx style="verb">forbidden</spanx> error.
</t>
<t>A mailbox MUST NOT be destroyed if it still has any child mailboxes. Attempts to do so MUST be rejected with a <spanx style="verb">mailboxHasChild</spanx> error.
</t>
<t>A mailbox MUST NOT be destroyed if it has any messages assigned to it. Attempts to do so MUST be rejected with a <spanx style="verb">mailboxHasMessage</spanx> error.
</t>
</section>

<section anchor="response" title="Response">
<t>The response to <spanx style="emph">setMailboxes</spanx> is called <spanx style="emph">mailboxesSet</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String|null</spanx>
The state string that would have been returned by <spanx style="verb">getMailboxes</spanx> before making the requested changes, or <spanx style="verb">null</spanx> if the server doesn't know what the previous state string was.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
The state string that will now be returned by <spanx style="verb">getMailboxes</spanx>.</t>
<t><spanx style="strong">created</spanx>: <spanx style="verb">String[Mailbox]</spanx>
A map of the creation id to an object containing all server-set properties for each successfully created Mailbox: <spanx style="emph">id</spanx>, <spanx style="emph">mustBeOnlyMailbox</spanx>, all <spanx style="emph">mayXXX</spanx> properties, <spanx style="emph">totalMessages</spanx>, <spanx style="emph">unreadMessages</spanx>, <spanx style="emph">totalThreads</spanx> and <spanx style="emph">unreadThreads</spanx>.</t>
<t><spanx style="strong">updated</spanx>: <spanx style="verb">String[Mailbox|null]</spanx>
The <spanx style="emph">keys</spanx> in this map are the ids of all mailboxes that were successfully updated. If the server made any other changes to the record beyond those explicitly requested by the client, the <spanx style="emph">value</spanx> for the corresponding id in the map is an object containing the updated value of each property the <spanx style="strong">server changed</spanx>. Otherwise (if no properties changed on the server other than those explicitly updated by the client), the value is <spanx style="verb">null</spanx>.</t>
<t><spanx style="strong">destroyed</spanx>: <spanx style="verb">String[]</spanx>
A list of ids for Mailboxes that were successfully destroyed.</t>
<t><spanx style="strong">notCreated</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of creation id to a SetError object for each Mailbox that failed to be created. The possible errors are defined above.</t>
<t><spanx style="strong">notUpdated</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of Mailbox id to a SetError object for each Mailbox that failed to be updated. The possible errors are defined above.</t>
<t><spanx style="strong">notDestroyed</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of Mailbox id to a SetError object for each Mailbox that failed to be destroyed. The possible errors are defined above.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="emph">mailboxesSet</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">accountReadOnly</spanx>: Returned if the account has <spanx style="verb">isReadOnly == true</spanx>.
</t>
<t><spanx style="verb">requestTooLarge</spanx>: Returned if the total number of objects to create, update or destroy exceeds the maximum number the server is willing to process in a single method call.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="emph">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t><spanx style="verb">stateMismatch</spanx>: Returned if an <spanx style="verb">ifInState</spanx> argument was supplied and it does not match the current state.
</t>
<t>Example request:
</t>

<figure align="center"><artwork align="center">
[ "setMailboxes", {
  "ifInState": "ms4123",
  "update": {
    "f3": {
      "name": "The new name"
    }
  },
  "destroy": [ "f5" ]
}, "#0" ]
</artwork></figure>
</section>
</section>
</section>

<section anchor="messagelists" title="MessageLists">
<t>A <spanx style="strong">MessageList</spanx> is a sorted query on the set of messages in a user's account. Since it can be very long, the client must specify what section of the list to return. The client can optionally also fetch the threads and/or messages for this part of the list.
</t>
<t>The same message may appear in multiple messages lists. For example, it may belong to multiple mailboxes, and of course it can appear in searches. Since messages have an immutable id, a client can easily tell if it already has a message cached and only fetch the ones it needs.
</t>
<t>When the state changes on the server, a delta update can be requested to efficiently update the client's cache of this list to the new state. If the server doesn't support this, the client still only needs to fetch the message list again, not the messages themselves.
</t>

<section anchor="getmessagelist" title="getMessageList">
<t>To fetch a message list, make a call to <spanx style="emph">getMessageList</spanx>. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If <spanx style="verb">null</spanx>, the primary account will be used.</t>
<t><spanx style="strong">filter</spanx>: <spanx style="verb">FilterCondition|FilterOperator|null</spanx>
Determines the set of messages returned in the results. See the &quot;Filtering&quot; section below for allowed values and semantics.</t>
<t><spanx style="strong">sort</spanx>: <spanx style="verb">String[]|null</spanx>
A list of Message property names to sort by. See the &quot;Sorting&quot; section below for allowed values and semantics.</t>
<t><spanx style="strong">collapseThreads</spanx>: <spanx style="verb">Boolean|null</spanx>
If true, each thread will only be returned once in the resulting list, at the position of the first message in the list (given the filter and sort order) belonging to the thread. If <spanx style="verb">false</spanx> or <spanx style="verb">null</spanx>, threads may be returned multiple times.</t>
<t><spanx style="strong">position</spanx>: <spanx style="verb">Number|null</spanx>
The 0-based index of the first result in the list to return. If a negative value is given, the call MUST be rejected with an <spanx style="verb">invalidArguments</spanx> error. If <spanx style="verb">null</spanx>, 0 is used.</t>
<t><spanx style="strong">anchor</spanx>: <spanx style="verb">String|null</spanx>
A Message id. The index of this message id will be used in combination with the <spanx style="verb">anchorOffset</spanx> argument to determine the index of the first result to return (see the &quot;Windowing&quot; section below for more details).</t>
<t><spanx style="strong">anchorOffset</spanx>: <spanx style="verb">Number|null</spanx>
The index of the anchor message relative to the index of the first result to return. This MAY be negative. For example, <spanx style="verb">-1</spanx> means the first message after the anchor message should be the first result in the results returned (see the &quot;Windowing&quot; section below for more details).</t>
<t><spanx style="strong">limit</spanx>: <spanx style="verb">Number|null</spanx>
The maximum number of results to return. If <spanx style="verb">null</spanx>, no limit is presumed. The server MAY choose to enforce a maximum <spanx style="verb">limit</spanx> argument. In this case, if a greater value is given, the limit should be clamped to the maximum; since the total number of results in the list is returned, the client should not be relying on how many results are returned to determine if it has reached the end of the list. If a negative value is given, the call MUST be rejected with an <spanx style="verb">invalidArguments</spanx> error.</t>
<t><spanx style="strong">fetchThreads</spanx>: <spanx style="verb">Boolean|null</spanx>
If <spanx style="verb">true</spanx>, after outputting a <spanx style="emph">messageList</spanx> response, an implicit call will be made to <spanx style="emph">getThreads</spanx> with the <spanx style="emph">threadIds</spanx> array in the response as the <spanx style="emph">ids</spanx> argument, and the <spanx style="emph">fetchMessages</spanx> and <spanx style="emph">fetchMessageProperties</spanx> arguments passed straight through from the call to <spanx style="emph">getMessageList</spanx>. If <spanx style="verb">false</spanx> or <spanx style="verb">null</spanx>, no implicit call will be made.</t>
<t><spanx style="strong">fetchMessages</spanx>: <spanx style="verb">Boolean|null</spanx>
If <spanx style="verb">true</spanx> and <spanx style="verb">fetchThreads == false</spanx>, then after outputting a <spanx style="emph">messageList</spanx> response, an implicit call will be made to <spanx style="emph">getMessages</spanx> with the <spanx style="verb">messageIds</spanx> array in the response as the <spanx style="emph">ids</spanx> argument, and the <spanx style="emph">fetchMessageProperties</spanx> argument as the <spanx style="emph">properties</spanx> argument. If <spanx style="verb">false</spanx> or <spanx style="verb">null</spanx>, no implicit call will be made.</t>
<t><spanx style="strong">fetchMessageProperties</spanx>: <spanx style="verb">String[]|null</spanx>
The list of properties to fetch on any fetched messages. See <spanx style="emph">getMessages</spanx> for a full description.</t>
<t><spanx style="strong">fetchSearchSnippets</spanx>: <spanx style="verb">Boolean|null</spanx>
If <spanx style="verb">true</spanx>, then after outputting a <spanx style="emph">messageList</spanx> and making any other implicit calls, an implicit call will be made to <spanx style="emph">getSearchSnippets</spanx>. The <spanx style="emph">messageIds</spanx> array from the response will be used as the <spanx style="emph">messageIds</spanx> argument, and the <spanx style="emph">filter</spanx> argument will be passed straight through. If <spanx style="verb">false</spanx> or <spanx style="verb">null</spanx>, no implicit call will be made.</t>
</list>
</t>

<section anchor="filtering" title="Filtering">
<t>A <spanx style="strong">FilterOperator</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">operator</spanx>: <spanx style="verb">String</spanx>
This MUST be one of the following strings: &quot;AND&quot;/&quot;OR&quot;/&quot;NOT&quot;:
<list style="symbols">
<t><spanx style="strong">AND</spanx>: all of the conditions must match for the filter to match.</t>
<t><spanx style="strong">OR</spanx>: at least one of the conditions must match for the filter to match.</t>
<t><spanx style="strong">NOT</spanx>: none of the conditions must match for the filter to match.</t>
</list></t>
<t><spanx style="strong">conditions</spanx>: <spanx style="verb">(FilterCondition|FilterOperator)[]</spanx>
The conditions to evaluate against each message.</t>
</list>
</t>
<t>A <spanx style="strong">FilterCondition</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">inMailbox</spanx>: <spanx style="verb">String|null</spanx>
A mailbox id. A message must be in this mailbox to match the condition.</t>
<t><spanx style="strong">inMailboxOtherThan</spanx>: <spanx style="verb">String|null</spanx>
A mailbox id. A message be in any mailbox other than this one to match the condition. This is to allow messages solely in trash/spam to be easily excluded from a search.</t>
<t><spanx style="strong">before</spanx>: <spanx style="verb">Date|null</spanx>
The date of the message (as returned on the Message object) must be before this date to match the condition.</t>
<t><spanx style="strong">after</spanx>: <spanx style="verb">Date|null</spanx>
The date of the message (as returned on the Message object) must be on or after this date to match the condition.</t>
<t><spanx style="strong">minSize</spanx>: <spanx style="verb">Number|null</spanx>
The size of the message in bytes (as returned on the Message object) must be equal to or greater than this number to match the condition.</t>
<t><spanx style="strong">maxSize</spanx>: <spanx style="verb">Number|null</spanx>
The size of the message in bytes (as returned on the Message object) must be less than this number to match the condition.</t>
<t><spanx style="strong">allInThreadHaveKeyword</spanx>: <spanx style="verb">String</spanx>
All messages (including this one) in the same thread as this message must have the given keyword to match the condition.</t>
<t><spanx style="strong">someInThreadHaveKeyword</spanx>: <spanx style="verb">String</spanx>
At least one message (possibly this one) in the same thread as this message must have the given keyword to match the condition.</t>
<t><spanx style="strong">noneInThreadHaveKeyword</spanx>: <spanx style="verb">String</spanx>
All messages (including this one) in the same thread as this message must <spanx style="strong">not</spanx> have the given keyword to match the condition.</t>
<t><spanx style="strong">hasKeyword</spanx>: <spanx style="verb">String</spanx>
This message must have the given keyword to match the condition.</t>
<t><spanx style="strong">notKeyword</spanx>: <spanx style="verb">String</spanx>
This message must not have the given keyword to match the condition.</t>
<t><spanx style="strong">hasAttachment</spanx>: <spanx style="verb">Boolean|null</spanx>
The <spanx style="verb">hasAttachment</spanx> property of the message must be identical to the value given to match the condition.</t>
<t><spanx style="strong">text</spanx>: <spanx style="verb">String|null</spanx>
Looks for the text in the <spanx style="emph">from</spanx>, <spanx style="emph">to</spanx>, <spanx style="emph">cc</spanx>, <spanx style="emph">bcc</spanx>, <spanx style="emph">subject</spanx>, <spanx style="emph">textBody</spanx> or <spanx style="emph">htmlBody</spanx> properties of the message.</t>
<t><spanx style="strong">from</spanx>: <spanx style="verb">String|null</spanx>
Looks for the text in the <spanx style="emph">from</spanx> property of the message.</t>
<t><spanx style="strong">to</spanx>: <spanx style="verb">String|null</spanx>
Looks for the text in the <spanx style="emph">to</spanx> property of the message.</t>
<t><spanx style="strong">cc</spanx>: <spanx style="verb">String|null</spanx>
Looks for the text in the <spanx style="emph">cc</spanx> property of the message.</t>
<t><spanx style="strong">bcc</spanx>: <spanx style="verb">String|null</spanx>
Looks for the text in the <spanx style="emph">bcc</spanx> property of the message.</t>
<t><spanx style="strong">subject</spanx>: <spanx style="verb">String|null</spanx>
Looks for the text in the <spanx style="emph">subject</spanx> property of the message.</t>
<t><spanx style="strong">body</spanx>: <spanx style="verb">String|null</spanx>
Looks for the text in the <spanx style="emph">textBody</spanx> or <spanx style="emph">htmlBody</spanx> property of the message.</t>
<t><spanx style="strong">header</spanx>: <spanx style="verb">String[]|null</spanx>
The array MUST contain either one or two elements. The first element is the name of the header to match against. The second (optional) element is the text to look for in the header. If not supplied, the message matches simply if it <spanx style="emph">has</spanx> a header of the given name.</t>
</list>
</t>
<t>If zero properties are specified on the FilterCondition, the condition MUST always evaluate to <spanx style="verb">true</spanx>. If multiple properties are specified, ALL must apply for the condition to be <spanx style="verb">true</spanx> (it is equivalent to splitting the object into one-property conditions and making them all the child of an AND filter operator).
</t>
<t>The exact semantics for matching <spanx style="verb">String</spanx> fields is <spanx style="strong">deliberately not defined</spanx> to allow for flexibility in indexing implementation, subject to the following:
</t>
<t>
<list style="symbols">
<t>Text SHOULD be matched in a case-insensitive manner.</t>
<t>Text contained in either (but matched) single or double quotes SHOULD be treated as a <spanx style="strong">phrase search</spanx>, that is a match is required for that exact word or sequence of words, excluding the surrounding quotation marks. Use <spanx style="verb">\"</spanx>, <spanx style="verb">\'</spanx> and <spanx style="verb">\\</spanx> to match a literal <spanx style="verb">"</spanx>, <spanx style="verb">'</spanx> and <spanx style="verb">\</spanx> respectively in a phrase.</t>
<t>Outside of a phrase, white-space SHOULD be treated as dividing separate tokens that may be searched for separately in the message, but MUST all be present for the message to match the filter.</t>
<t>Tokens MAY be matched on a whole-word basis using stemming (so for example a text search for <spanx style="verb">bus</spanx> would match &quot;buses&quot; but not &quot;business&quot;).</t>
<t>When searching inside the <spanx style="emph">htmlBody</spanx> property, HTML tags and attributes SHOULD be ignored.</t>
</list>
</t>
</section>

<section anchor="sorting" title="Sorting">
<t>The <spanx style="verb">sort</spanx> argument lists the properties to compare between two messages to determine which comes first in the sort. If two messages have an identical value for the first property, the next property will be considered and so on. If all properties are the same (this includes the case where an empty array or <spanx style="verb">null</spanx> is given as the argument), the sort order is server-dependent, but MUST be stable between calls to <spanx style="verb">getMessageList</spanx>.
</t>
<t>Following the property name there MUST be a space and then either the string <spanx style="verb">asc</spanx> or <spanx style="verb">desc</spanx> to specify ascending or descending sort for that property.
</t>
<t>The following properties MUST be supported for sorting:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">id</spanx> - The id as returned in the Message object.</t>
<t><spanx style="strong">date</spanx> - The date as returned in the Message object.</t>
</list>
</t>
<t>The following properties SHOULD be supported for sorting:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">size</spanx> - The size as returned in the Message object.</t>
<t><spanx style="strong">from</spanx> – This is taken to be either the &quot;name&quot; part of the Emailer object, or if none then the &quot;email&quot; part of the Emailer object (see the definition of the from property in the Message object). If still none, consider the value to be the empty string.</t>
<t><spanx style="strong">to</spanx> - This is taken to be either the &quot;name&quot; part of the <spanx style="strong">first</spanx> Emailer object, or if none then the &quot;email&quot; part of the <spanx style="strong">first</spanx> Emailer object (see the definition of the to property in the Message object). If still none, consider the value to be the empty string.</t>
<t><spanx style="strong">subject</spanx> - This is taken to be the subject of the Message with any ignoring any leading &quot;Fwd:&quot;s or &quot;Re:&quot;s (case-insensitive match).</t>
<t><spanx style="strong">keyword:</spanx><spanx style="verb">$keyword</spanx> - This value MUST be considered <spanx style="verb">true</spanx> if the message has the keyword, or <spanx style="verb">false</spanx> otherwise.</t>
<t><spanx style="strong">allThreadKeyword:</spanx><spanx style="verb">$keyword</spanx> - This value MUST be considered <spanx style="verb">true</spanx> for the message if <spanx style="strong">all</spanx> of the messages in the same thread (regardless of mailbox) have the keyword.</t>
<t><spanx style="strong">someThreadKeyword:</spanx><spanx style="verb">$keyword</spanx> - This value MUST be considered <spanx style="verb">true</spanx> for the message if <spanx style="strong">any</spanx> of the messages in the same thread (regardless of mailbox) have the keyword.</t>
</list>
</t>
<t>The server MAY support sorting based on other properties as well. A client can discover which properties are supported by inspecting the server's <spanx style="emph">capabilities</spanx> object (see section 1).
</t>
<t>The method of comparison depends on the type of the property:
</t>
<t>
<list style="symbols">
<t><spanx style="verb">String</spanx>: Comparison function is server-dependent. It SHOULD be case-insensitive and SHOULD take into account locale-specific conventions if known for the user. However, the server MAY choose to just sort based on unicode code point, after best-effort translation to lower-case.</t>
<t><spanx style="verb">Date</spanx>: If sorting in ascending order, the earlier date MUST come first.</t>
<t><spanx style="verb">Boolean</spanx>: If sorting in ascending order, a <spanx style="verb">false</spanx> value MUST come before a <spanx style="verb">true</spanx> value.</t>
</list>
</t>
<t>Example sort:
</t>

<figure align="center"><artwork align="center">
`[ "someThreadKeyword:$Flagged desc", "date desc" ]
</artwork></figure>
<t>This would sort messages in flagged threads first (the thread is considered flagged if any message within it is flagged), and then in date order, newest first. If two messages have both identical flagged status and date, the order is server-dependent but must be stable.
</t>
</section>

<section anchor="thread-collapsing" title="Thread collapsing">
<t>When <spanx style="verb">collapseThreads == true</spanx>, then after filtering and sorting the message list, the list is further winnowed by removing any messages for a thread id that has already been seen (when passing through the list sequentially). A thread will therefore only appear <spanx style="strong">once</spanx> in the <spanx style="verb">threadIds</spanx> list of the result, at the position of the first message in the list that belongs to the thread.
</t>
</section>

<section anchor="windowing" title="Windowing">
<t>If a <spanx style="emph">position</spanx> offset is supplied, then this is the 0-based index of the first result to return in the list of messages after filtering, sorting and collapsing threads. If the index is greater than or equal to the total number of messages in the list, then there are no results to return, but this DOES NOT generate an error. If <spanx style="emph">position</spanx> is <spanx style="verb">null</spanx> (or, equivalently, omitted) this MUST be interpreted as <spanx style="verb">position: 0</spanx>.
</t>
<t>Alternatively, a message id, called the <spanx style="strong">anchor</spanx> may be given. In this case, after filtering, sorting and collapsing threads, the anchor is searched for in the message list. If found, the <spanx style="strong">anchor offset</spanx> is then subtracted from this index. If the resulting index is now negative, it is clamped to 0. This index is now used exactly as though it were supplied as the <spanx style="verb">position</spanx> argument. If the anchor is not found, the call is rejected with an <spanx style="verb">anchorNotFound</spanx> error.
</t>
<t>If an <spanx style="emph">anchor</spanx> is specified, any position argument supplied by the client MUST be ignored. If <spanx style="emph">anchorOffset</spanx> is <spanx style="verb">null</spanx>, it defaults to <spanx style="verb">0</spanx>. If no <spanx style="emph">anchor</spanx> is supplied, any anchor offset argument MUST be ignored.
</t>
</section>

<section anchor="response-1" title="Response">
<t>The response to a call to <spanx style="emph">getMessageList</spanx> is called <spanx style="emph">messageList</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">filter</spanx>: <spanx style="verb">FilterCondition|FilterOperator|null</spanx>
The filter of the message list. Echoed back from the call.</t>
<t><spanx style="strong">sort</spanx>: <spanx style="verb">String[]|null</spanx>
A list of Message property names used to sort by. Echoed back from the call.</t>
<t><spanx style="strong">collapseThreads</spanx>: <spanx style="verb">Boolean|null</spanx>
Echoed back from the call.</t>
<t><spanx style="strong">state</spanx>: <spanx style="verb">String</spanx>
A string encoding the current state on the server. This string will change if the results of the message list MAY have changed (for example, there has been a change to the state of the set of Messages; it does not guarantee that anything in the list has changed). It may be passed to <spanx style="emph">getMessageListUpdates</spanx> to efficiently get the set of changes from the previous state.
Should a client receive back a response with a different state string to a previous call, it MUST either throw away the currently cached list and fetch it again (note, this does not require fetching the messages again, just the list of ids) or, if the server supports it, call <spanx style="emph">getMessageListUpdates</spanx> to get the delta difference.</t>
<t><spanx style="strong">canCalculateUpdates</spanx>: <spanx style="verb">Boolean</spanx>
This is <spanx style="verb">true</spanx> if the server supports calling <spanx style="verb">getMessageListUpdates</spanx> with these <spanx style="verb">filter</spanx>/<spanx style="verb">sort</spanx>/<spanx style="verb">collapseThreads</spanx> parameters. Note, this does not guarantee that the getMessageListUpdates call will succeed, as it may only be possible for a limited time afterwards due to server internal implementation details.</t>
<t><spanx style="strong">position</spanx>: <spanx style="verb">Number</spanx>
The 0-based index of the first result in the <spanx style="verb">threadIds</spanx> array within the complete list.</t>
<t><spanx style="strong">total</spanx>: <spanx style="verb">Number</spanx>
The total number of messages in the message list (given the <spanx style="emph">filter</spanx> and <spanx style="emph">collapseThreads</spanx> arguments).</t>
<t><spanx style="strong">threadIds</spanx>: <spanx style="verb">String[]</spanx>
The list of Thread ids for each message in the list after filtering, sorting and collapsing threads, starting at the index given by the <spanx style="emph">position</spanx> argument of this response, and continuing until it hits the end of the list or reaches the <spanx style="verb">limit</spanx> number of ids.</t>
<t><spanx style="strong">messageIds</spanx>: <spanx style="verb">String[]</spanx>
The list of Message ids for each message in the list after filtering, sorting and collapsing threads, starting at the index given by the <spanx style="emph">position</spanx> argument of this response, and continuing until it hits the end of the list or reaches the <spanx style="verb">limit</spanx> number of ids.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="verb">messageList</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">unsupportedSort</spanx>: Returned if the <spanx style="emph">sort</spanx> includes a property the server does not support sorting on.
</t>
<t><spanx style="verb">cannotDoFilter</spanx>: Returned if the server is unable to process the given <spanx style="emph">filter</spanx> for any reason.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if the request does not include one of the required arguments, or one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="verb">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t><spanx style="verb">anchorNotFound</spanx>: Returned if an anchor argument was supplied, but it cannot be found in the message list.
</t>
</section>
</section>

<section anchor="getmessagelistupdates" title="getMessageListUpdates">
<t>The <spanx style="verb">getMessageListUpdates</spanx> call allows a client to efficiently update the state of any cached message list to match the new state on the server. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If <spanx style="verb">null</spanx>, the primary account will be used.</t>
<t><spanx style="strong">filter</spanx>: <spanx style="verb">FilterCondition|FilterOperator|null</spanx>
The filter argument that was used with <spanx style="emph">getMessageList</spanx>.</t>
<t><spanx style="strong">sort</spanx>: <spanx style="verb">String[]|null</spanx>
The sort argument that was used with <spanx style="emph">getMessageList</spanx>.</t>
<t><spanx style="strong">collapseThreads</spanx>: <spanx style="verb">Boolean|null</spanx>
The <spanx style="emph">collapseThreads</spanx> argument that was used with <spanx style="emph">getMessageList</spanx>.</t>
<t><spanx style="strong">sinceState</spanx>: <spanx style="verb">String</spanx>
The current state of the client. This is the string that was returned as the <spanx style="emph">state</spanx> argument in the <spanx style="emph">messageList</spanx> response. The server will return the changes made since this state.</t>
<t><spanx style="strong">uptoMessageId</spanx>: <spanx style="verb">String|null</spanx>
The message id of the last message in the list that the client knows about. In the common case of the client only having the first X ids cached, this allows the server to ignore changes further down the list the client doesn't care about.</t>
<t><spanx style="strong">maxChanges</spanx>: <spanx style="verb">Number|null</spanx>
The maximum number of changes to return in the response. See below for a more detailed description.</t>
</list>
</t>
<t>The response to <spanx style="emph">getMessageListUpdates</spanx> is called <spanx style="emph">messageListUpdates</spanx> It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">filter</spanx>: <spanx style="verb">FilterCondition|FilterOperator|null</spanx>
The filter of the message list. Echoed back from the call.</t>
<t><spanx style="strong">sort</spanx>: <spanx style="verb">String[]|null</spanx>
A list of Message property names used to sort by. Echoed back from the call.</t>
<t><spanx style="strong">collapseThreads</spanx>: <spanx style="verb">Boolean|null</spanx>
Echoed back from the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String</spanx>
This is the <spanx style="verb">sinceState</spanx> argument echoed back; the state from which the server is returning changes.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
This is the state the client will be in after applying the set of changes to the old state.</t>
<t><spanx style="strong">uptoMessageId</spanx>: <spanx style="verb">String|null</spanx>
Echoed back from the call.</t>
<t><spanx style="strong">total</spanx>: <spanx style="verb">Number</spanx>
The total number of messages in the message list (given the filter and collapseThreads arguments).</t>
<t><spanx style="strong">removed</spanx>: <spanx style="verb">RemovedItem[]</spanx>
The <spanx style="emph">messageId</spanx> and <spanx style="emph">threadId</spanx> for every message that was in the list in the old state and is not in the list in the new state. If the server cannot calculate this exactly, the server MAY return extra messages in addition that MAY have been in the old list but are not in the new list.
If an <spanx style="emph">uptoMessageId</spanx> was given AND this id was found in the list, only messages positioned before this message that were removed need be returned.
In addition, if the sort includes a keyword, the server MUST include all messages in the current list for which this keyword MAY have changed. If the sort includes a &quot;some/all-in-thread-keyword&quot;, then the server MUST include all messages in the current list for which this keyword MAY have changed <spanx style="strong">on any of the messages in the thread</spanx>.</t>
<t><spanx style="strong">added</spanx>: <spanx style="verb">AddedItem[]</spanx>
The messageId and threadId and index in the list (in the new state) for every message that has been added to the list since the old state AND every message in the current list that was included in the <spanx style="emph">removed</spanx> array (due to a filter or sort based upon a mutable property). The array MUST be sorted in order of index, lowest index first.
If an <spanx style="emph">uptoMessageId</spanx> was given AND this id was found in the list, only messages positioned before this message that have been added need be returned.</t>
</list>
</t>
<t>A <spanx style="strong">RemovedItem</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">messageId</spanx>: <spanx style="verb">String</spanx></t>
<t><spanx style="strong">threadId</spanx>: <spanx style="verb">String</spanx></t>
</list>
</t>
<t>An <spanx style="strong">AddedItem</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">messageId</spanx>: <spanx style="verb">String</spanx></t>
<t><spanx style="strong">threadId</spanx>: <spanx style="verb">String</spanx></t>
<t><spanx style="strong">index</spanx>: <spanx style="verb">Number</spanx></t>
</list>
</t>
<t>The result of this should be that if the client has a cached sparse array of message ids in the list in the old state:
</t>

<figure align="center"><artwork align="center">
messageIds = [ "id1", "id2", null, null, "id3", "id4", null, null, null ]
</artwork></figure>
<t>then if it <spanx style="strong">splices out</spanx> all messages in the removed array:
</t>

<figure align="center"><artwork align="center">
removed = [{ messageId: "id2", … }];
messageIds =&gt; [ "id1", null, null, "id3", "id4", null, null, null ]
</artwork></figure>
<t>and <spanx style="strong">splices in</spanx> (in order) all of the messages in the added array:
</t>

<figure align="center"><artwork align="center">
added = [{ messageId: "id5", index: 0, … }];
messageIds =&gt; [ "id5", "id1", null, null, "id3", "id4", null, null, null ]
</artwork></figure>
<t>then the message list will now be in the new state.
</t>
<t>The following errors may be returned instead of the <spanx style="verb">messageListUpdates</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if the request does not include one of the required arguments, or one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="emph">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t><spanx style="verb">tooManyChanges</spanx>: Returned if there are more changes the the client's <spanx style="emph">maxChanges</spanx> argument. Each item in the removed or added array is considered as one change. The client may retry with a higher max changes or invalidate its cache of the message list.
</t>
<t><spanx style="verb">cannotCalculateChanges</spanx>: Returned if the server cannot calculate the changes from the state string given by the client. Usually due to the client's state being too old. The client MUST invalidate its cache of the message list.
</t>
</section>
</section>

<section anchor="threads" title="Threads">
<t>Replies are grouped together with the original message to form a thread. In JMAP, a thread is simply a flat list of messages, ordered by date. Every message MUST belong to a thread, even if it is the only message in the thread.
</t>
<t>The JMAP spec does not require the server to use any particular algorithm for determining whether two messages belong to the same thread, however there is a recommended algorithm in the <eref target="server.html">implementation guide</eref>.
</t>
<t>If messages are delivered out of order for some reason, a user may receive two messages in the same thread but without headers that associate them with each other. The arrival of a third message in the thread may provide the missing references to join them all together into a single thread. Since the <spanx style="verb">threadId</spanx> of a message is immutable, if the server wishes to merge the threads, it MUST handle this by deleting and reinserting (with a new message id) the messages that change threadId.
</t>
<t>A <spanx style="strong">Thread</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">id</spanx>: <spanx style="verb">String</spanx>
The id of the thread. This property is immutable.</t>
<t><spanx style="strong">messageIds</spanx>: <spanx style="verb">String[]</spanx>
The ids of the messages in the thread, sorted such that:
<list style="symbols">
<t>Any message with the <spanx style="verb">$Draft</spanx> keyword that has an <spanx style="verb">In-Reply-To</spanx> header is sorted after the <spanx style="emph">first</spanx> non-draft message in the thread with the corresponding <spanx style="verb">Message-Id</spanx> header, but before any subsequent non-draft messages.</t>
<t>Other than that, everything is sorted in <spanx style="emph">date</spanx> order (as determined by the date property on the <spanx style="emph">Message</spanx> object), oldest first.</t>
<t>If two messages are identical under the above two conditions, the sort is server-dependent but MUST be stable (sorting by id is recommended).</t>
</list></t>
</list>
</t>

<section anchor="getthreads" title="getThreads">
<t>Threads can only be fetched explicitly by id. To fetch threads, make a call to <spanx style="emph">getThreads</spanx>. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If not given, defaults to the primary account.</t>
<t><spanx style="strong">ids</spanx>: <spanx style="verb">String[]</spanx>
An array of ids for the threads to fetch.</t>
<t><spanx style="strong">fetchMessages</spanx>: <spanx style="verb">Boolean|null</spanx>
If true, after outputting a <spanx style="emph">threads</spanx> response, an implicit call will be made to <spanx style="emph">getMessages</spanx> with a list of all message ids in the returned threads as the <spanx style="emph">ids</spanx> argument, and the <spanx style="emph">fetchMessageProperties</spanx> argument as the <spanx style="emph">properties</spanx> argument. If <spanx style="verb">false</spanx> or <spanx style="verb">null</spanx>, no implicit call will be made.</t>
<t><spanx style="strong">fetchMessageProperties</spanx>: <spanx style="verb">String[]|null</spanx>
The list of properties to fetch on any fetched messages. See <spanx style="emph">getMessages</spanx> for a full description.</t>
</list>
</t>
<t>The response to <spanx style="emph">getThreads</spanx> is called <spanx style="emph">threads</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">state</spanx>: <spanx style="verb">String</spanx>
A string encoding the current state on the server. This string will change
if any threads change (that is, new messages arrive, or messages are deleted, as these are the only two events that change thread membership). It can be passed to <spanx style="emph">getThreadUpdates</spanx> to efficiently get the list of changes from the previous state.</t>
<t><spanx style="strong">list</spanx>: <spanx style="verb">Thread[]</spanx>
An array of Thread objects for the requested thread ids. This may not be in the same order as the ids were in the request.</t>
<t><spanx style="strong">notFound</spanx>: <spanx style="verb">String[]|null</spanx>
An array of thread ids requested which could not be found, or <spanx style="verb">null</spanx> if all ids were found.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="verb">threads</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">requestTooLarge</spanx>: Returned if the number of <spanx style="emph">ids</spanx> requested by the client exceeds the maximum number the server is willing to process in a single method call.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if the request does not include one of the required arguments, or one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="emph">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t>Example of a successful request:
</t>

<figure align="center"><artwork align="center">
[ "getThreads", {
  "ids": ["f123u4", "f41u44"],
  "fetchMessages": false,
  "fetchMessageProperties": null
}, "#1" ]
</artwork></figure>
<t>and response:
</t>

<figure align="center"><artwork align="center">
[ "threads", {
  "state": "f6a7e214",
  "list": [
    {
      "id": "f123u4",
      "messageIds": [ "eaa623", "f782cbb"]
    },
    {
      "id": "f41u44",
      "messageIds": [ "82cf7bb" ]
    }
  ],
  "notFound": null
}, "#1" ]
</artwork></figure>
</section>

<section anchor="getthreadupdates" title="getThreadUpdates">
<t>When messages are created or deleted, new threads may be created, or the set of messages belonging to an existing thread may change. If a call to <spanx style="emph">getThreads</spanx> returns with a different <spanx style="emph">state</spanx> string in the response to a previous call, the state of the threads has changed on the server and the client needs to work out which part of its cache is now invalid.
</t>
<t>The <spanx style="emph">getThreadUpdates</spanx> call allows a client to efficiently update the state of any cached threads to match the new state on the server. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If not given, defaults to the primary account.</t>
<t><spanx style="strong">sinceState</spanx>: <spanx style="verb">String</spanx>
The current state of the client. This is the string that was returned as the <spanx style="emph">state</spanx> argument in the <spanx style="emph">threads</spanx> response. The server will return the changes made since this state.</t>
<t><spanx style="strong">maxChanges</spanx>: <spanx style="verb">Number|null</spanx>
The maximum number of Thread ids to return in the response. The server MAY choose to clamp this value to a particular maximum or set a maximum if none is given by the client. If supplied by the client, the value MUST be a positive integer greater than 0. If a value outside of this range is given, the server MUST reject the call with an <spanx style="verb">invalidArguments</spanx> error.</t>
<t><spanx style="strong">fetchRecords</spanx>: <spanx style="verb">Boolean|null</spanx>
If <spanx style="verb">true</spanx>, immediately after outputting a <spanx style="emph">threadUpdates</spanx> response, an implicit call will be made to <spanx style="emph">getThreads</spanx> with the <spanx style="emph">changed</spanx> property of the response as the <spanx style="emph">ids</spanx> argument, and <spanx style="emph">fetchMessages</spanx> equal to <spanx style="verb">false</spanx>.</t>
</list>
</t>
<t>The response to <spanx style="emph">getThreadUpdates</spanx> is called <spanx style="emph">threadUpdates</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String</spanx>
This is the <spanx style="emph">sinceState</spanx> argument echoed back; the state from which the server is returning changes.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
This is the state the client will be in after applying the set of changes to the old state.</t>
<t><spanx style="strong">hasMoreUpdates</spanx>: <spanx style="verb">Boolean</spanx>
If <spanx style="verb">true</spanx>, the client may call <spanx style="emph">getThreadUpdates</spanx> again with the <spanx style="emph">newState</spanx> returned to get further updates. If <spanx style="verb">false</spanx>, <spanx style="emph">newState</spanx> is the current server state.</t>
<t><spanx style="strong">changed</spanx>: <spanx style="verb">String[]</spanx>
An array of thread ids where the list of messages within the thread has
changed between the old state and the new state, and the thread currently has at least one message in it.</t>
<t><spanx style="strong">removed</spanx>: <spanx style="verb">String[]</spanx>
An array of thread ids where the list of messages within the thread has changed since the old state, and there are now no messages in the thread.</t>
</list>
</t>
<t>If a <spanx style="emph">maxChanges</spanx> is supplied, or set automatically by the server, the server MUST ensure the number of ids returned across <spanx style="emph">changed</spanx> and <spanx style="emph">removed</spanx> does not exceed this limit. If there are more changes than this between the client's state and the current server state, the update returned SHOULD generate an update to take the client to an intermediate state, from which the client can continue to call <spanx style="emph">getThreadUpdates</spanx> until it is fully up to date. If it is unable to calculate an intermediate state, it MUST return a <spanx style="verb">cannotCalculateChanges</spanx> error response instead.
</t>
<t>If a thread has been modified AND deleted since the oldState, the server SHOULD just return the id in the <spanx style="emph">removed</spanx> response, but MAY return it in the changed response as well. If a thread has been created AND deleted since the oldState, the server SHOULD remove the thread id from the response entirely, but MAY include it in the <spanx style="emph">removed</spanx> response.
</t>
<t>The following errors may be returned instead of the <spanx style="emph">threadUpdates</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if the request does not include one of the required arguments, or one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="emph">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t><spanx style="verb">cannotCalculateChanges</spanx>: Returned if the server cannot calculate the changes from the state string given by the client. Usually due to the client's state being too old, or the server being unable to produce an update to an intermediate state when there are too many updates. The client MUST invalidate its Thread cache.
</t>
</section>
</section>

<section anchor="messages" title="Messages">
<t>Just like in IMAP, a message is <spanx style="strong">immutable</spanx> except for the boolean <spanx style="verb">isXXX</spanx> status properties and the set of mailboxes it is in. This allows for more efficient caching of messages, and gives easier backwards compatibility for servers implementing an IMAP interface to the same data.
</t>
<t>JMAP completely hides the complexities of MIME. All special encodings of either headers or textual body parts, such as <eref target="https://tools.ietf.org/html/rfc4648">base64</eref>, or <eref target="http://tools.ietf.org/html/rfc2047">RFC 2047</eref> encoding of non-ASCII characters, MUST be fully decoded into UTF-8.
</t>
<t>A <spanx style="strong">Message</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">id</spanx>: <spanx style="verb">String</spanx>
The id of the message.</t>
<t><spanx style="strong">blobId</spanx>: <spanx style="verb">String</spanx>
The id representing the raw <xref target="RFC5322"/> message. This may be used to download
the original message or to attach it directly to another message etc.</t>
<t><spanx style="strong">threadId</spanx>: <spanx style="verb">String</spanx>
The id of the thread to which this message belongs.</t>
<t><spanx style="strong">mailboxIds</spanx>: <spanx style="verb">String[]</spanx> (Mutable)
The ids of the mailboxes the message is in. A message MUST belong to one or more mailboxes at all times (until it is deleted).</t>
<t><spanx style="strong">keywords</spanx>: <spanx style="verb">String[Boolean]</spanx> (Mutable)
A set of keywords that apply to the message. The set is represented as an object, with the keys being the <spanx style="emph">keywords</spanx>. The value for each key in the object MUST be <spanx style="verb">true</spanx>.
Keywords are shared with IMAP. The six system keywords from IMAP are treated specially. The following four keywords have their first character changed from <spanx style="verb">\</spanx> in IMAP to <spanx style="verb">$</spanx> in JMAP and have particular semantic meaning:
<list style="symbols">
<t><spanx style="verb">$Draft</spanx>: The message is a draft the user is composing.</t>
<t><spanx style="verb">$Seen</spanx>: The message has been read.</t>
<t><spanx style="verb">$Flagged</spanx>: The message has been flagged for urgent/special attention.</t>
<t><spanx style="verb">$Answered</spanx>: The message has been replied to.</t>
</list>
The IMAP <spanx style="verb">\Recent</spanx> keyword is not exposed via JMAP. The IMAP <spanx style="verb">\Deleted</spanx> keyword is also not present: IMAP uses a delete+expunge model, which JMAP does not. Any message with the <spanx style="verb">\Deleted</spanx> keyword MUST NOT be visible via JMAP.
Users may add arbitrary keywords to a message. For compatibility with IMAP, a keyword is a (case-sensitive) string of 1–255 characters in the ASCII subset %x21–%x127 (excludes control chars and space), and MUST NOT include any of these characters: <spanx style="verb">()]{%*"\</spanx>
The <eref target="https://www.iana.org/assignments/imap-keywords/imap-keywords.xhtml">IANA Keyword Registry</eref> as established in <xref target="RFC5788"/> assigns semantic meaning to some other keywords in common use. New keywords may be established here in the future. In particular, note:
<list style="symbols">
<t><spanx style="verb">$Forwarded</spanx>: The message has been forwarded.</t>
<t><spanx style="verb">$Phishing</spanx>: The message is highly likely to be phishing. Clients SHOULD warn users to take care when viewing this message and disable links and attachments.</t>
<t><spanx style="verb">$Junk</spanx>: The message is definitely spam. Clients SHOULD set this flag when users report spam to help train automated spam-detection systems.</t>
<t><spanx style="verb">$NotJunk</spanx>: The message is definitely not spam. Clients SHOULD set this flag when users indicate a message is legitimate, to help train automated spam-detection systems.</t>
</list></t>
<t><spanx style="strong">hasAttachment</spanx>: <spanx style="verb">Boolean</spanx>
Does the message have any attachments?</t>
<t><spanx style="strong">headers</spanx>: <spanx style="verb">String[String]</spanx>
A map of lower-cased header name to (decoded) header value for all headers in the message. For headers that occur multiple times (e.g. <spanx style="verb">Received</spanx>), the values are concatenated with a single new line (<spanx style="verb">\n</spanx>) character in between each one.</t>
<t><spanx style="strong">sender</spanx>: <spanx style="verb">Emailer|null</spanx>
An Emailer object (see below) containing the name/email from the parsed <spanx style="verb">Sender</spanx> header of the email. If the email doesn't have a <spanx style="verb">Sender</spanx> header, this is <spanx style="verb">null</spanx>.</t>
<t><spanx style="strong">from</spanx>: <spanx style="verb">Emailer[]|null</spanx>
An array of name/email objects (see below) representing the parsed <spanx style="verb">From</spanx> header of the email, in the same order as they appear in the header. If the email doesn't have a <spanx style="verb">From</spanx> header, this is <spanx style="verb">null</spanx>. If the header exists but does not have any content, the response is an array of zero length.</t>
<t><spanx style="strong">to</spanx>:  <spanx style="verb">Emailer[]|null</spanx>
An array of name/email objects (see below) representing the parsed <spanx style="verb">To</spanx> header of the email, in the same order as they appear in the header. If the email doesn't have a <spanx style="verb">To</spanx> header, this is <spanx style="verb">null</spanx>. If the header exists but does not have any content, the response is an array of zero length.</t>
<t><spanx style="strong">cc</spanx>:  <spanx style="verb">Emailer[]|null</spanx>
An array of name/email objects (see below) representing the parsed <spanx style="verb">Cc</spanx> header of the email, in the same order as they appear in the header. If the email doesn't have a <spanx style="verb">Cc</spanx> header, this is <spanx style="verb">null</spanx>. If the header exists but does not have any content, the response is an array of zero length.</t>
<t><spanx style="strong">bcc</spanx>:  <spanx style="verb">Emailer[]|null</spanx>
An array of name/email objects (see below) representing the parsed <spanx style="verb">Bcc</spanx> header of the email. If the email doesn't have a <spanx style="verb">Bcc</spanx> header (which will be true for most emails outside of the Sent mailbox), this is <spanx style="verb">null</spanx>. If the header exists but does not have any content, the response is an array of zero length.</t>
<t><spanx style="strong">replyTo</spanx>: <spanx style="verb">Emailer[]|null</spanx>
An array of name/email objects (see below) representing the parsed <spanx style="verb">Reply-To</spanx> header of the email, in the same order as they appear in the header. If the email doesn't have a <spanx style="verb">Reply-To</spanx> header, this is <spanx style="verb">null</spanx>. If the header exists but does not have any content, the response is an array of zero length.</t>
<t><spanx style="strong">subject</spanx>: <spanx style="verb">String</spanx>
The subject of the message.</t>
<t><spanx style="strong">date</spanx>: <spanx style="verb">Date</spanx>
The date the message was sent (or saved, if the message is a draft).</t>
<t><spanx style="strong">size</spanx>: <spanx style="verb">Number</spanx>
The size in bytes of the whole message as counted by the server towards the user's quota.</t>
<t><spanx style="strong">preview</spanx>: <spanx style="verb">String</spanx>
Up to 256 characters of the beginning of a plain text version of the message body. This is intended to be shown as a preview line on a mailbox listing, and the server may choose to skip quoted sections or salutations to return a more useful preview.</t>
<t><spanx style="strong">textBody</spanx>: <spanx style="verb">String</spanx>
The plain text body part for the message. If there is only an HTML version of the body, a plain text version MUST be generated from this; the exact method of conversion in this case is not defined and is server-specific. If there is neither a <spanx style="verb">text/plain</spanx> nor a <spanx style="verb">text/html</spanx> body part, this MUST be the empty string.</t>
<t><spanx style="strong">htmlBody</spanx>: <spanx style="verb">String|null</spanx>
The HTML body part for the message if present.</t>
<t><spanx style="strong">attachments</spanx>: <spanx style="verb">Attachment[]|null</spanx>
An array of attachment objects (see below) detailing all the attachments to the message.</t>
<t><spanx style="strong">attachedMessages</spanx>: <spanx style="verb">String[Message]|null</spanx>
An object mapping attachment id (as found in the <spanx style="verb">attachments</spanx> property) to a <spanx style="strong">Message</spanx> object with the following properties, for each <xref target="RFC5322"/> message attached to this one:
<list style="symbols">
<t>headers</t>
<t>from</t>
<t>to</t>
<t>cc</t>
<t>bcc</t>
<t>replyTo</t>
<t>subject</t>
<t>date</t>
<t>textBody</t>
<t>htmlBody</t>
<t>attachments</t>
<t>attachedMessages</t>
</list></t>
</list>
</t>
<t>An <spanx style="strong">Emailer</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">name</spanx>: <spanx style="verb">String</spanx>
The name of the sender/recipient. If a name cannot be extracted for an email, this property SHOULD be the empty string.</t>
<t><spanx style="strong">email</spanx>: <spanx style="verb">String</spanx>
The email address of the sender/recipient. This MUST be of the form <spanx style="verb">"&lt;mailbox&gt;@&lt;host&gt;"</spanx> If a <spanx style="verb">host</spanx> or even <spanx style="verb">mailbox</spanx> cannot be extracted for an email, the empty string SHOULD be used for this part (so the result MUST always still contain an <spanx style="verb">"@"</spanx> character).</t>
</list>
</t>
<t>Group information and comments from the RFC 5322 header MUST be discarded when converting into an Emailer object.
</t>
<t>Example array of Emailer objects:
</t>

<figure align="center"><artwork align="center">
[
    {name:"Joe Bloggs", email:"joeb@example.com"},
    {name:"", email:"john@example.com"},
    {name:"John Smith", email: "john@"}
]
</artwork></figure>
<t>An <spanx style="strong">Attachment</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">blobId</spanx>: <spanx style="verb">String</spanx>
The id of the binary data.</t>
<t><spanx style="strong">type</spanx>: <spanx style="verb">String</spanx>
The content-type of the attachment.</t>
<t><spanx style="strong">name</spanx>: <spanx style="verb">String|null</spanx>
The full file name, e.g. &quot;myworddocument.doc&quot;, if available.</t>
<t><spanx style="strong">size</spanx>: <spanx style="verb">Number</spanx>
The size, in bytes, of the attachment when fully decoded (i.e. the number of bytes in the file the user would download).</t>
<t><spanx style="strong">cid</spanx>: <spanx style="verb">String|null</spanx>
The id used within the message body to reference this attachment. This is only unique when paired with the message id, and has no meaning without reference to that.</t>
<t><spanx style="strong">isInline</spanx>: <spanx style="verb">Boolean</spanx>
True if the attachment is referenced by a <spanx style="verb">cid:</spanx> link from within the HTML body of the message.</t>
<t><spanx style="strong">width</spanx>: <spanx style="verb">Number|null</spanx> (optional, server MAY omit if not supported)
The width (in px) of the image, if the attachment is an image.</t>
<t><spanx style="strong">height</spanx>: <spanx style="verb">Number|null</spanx> (optional, server MAY omit if not supported)
The height (in px) of the image, if the attachment is an image.</t>
</list>
</t>

<section anchor="getmessages" title="getMessages">
<t>Messages can only be fetched explicitly by id. To fetch messages, make a call to <spanx style="verb">getMessages</spanx>. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If not given, defaults to the primary account.</t>
<t><spanx style="strong">ids</spanx>: <spanx style="verb">String[]</spanx>
An array of ids for the messages to fetch.</t>
<t><spanx style="strong">properties</spanx>: <spanx style="verb">String[]|null</spanx>
A list of properties to fetch for each message. If <spanx style="verb">null</spanx>, all properties will be fetched.</t>
</list>
</t>
<t>The <spanx style="verb">id</spanx> property is always returned, regardless of whether it is in the list of requested properties. The possible values for <spanx style="verb">properties</spanx> can be found above in the description of the Message object. In addition to this, the client may request the following special values:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">body</spanx>: If <spanx style="verb">"body"</spanx> is included in the list of requested properties, it will be interpreted by the server as a request for <spanx style="verb">"htmlBody"</spanx> if the message has an HTML part, or <spanx style="verb">"textBody"</spanx> otherwise.</t>
<t><spanx style="strong">headers.property</spanx>: Instead of requesting all the headers (by requesting the <spanx style="verb">"headers"</spanx> property, the client may specify the particular headers it wants using the <spanx style="verb">headers.property-name</spanx> syntax, e.g. <spanx style="verb">"headers.x-spam-score", "headers.x-spam-hits"</spanx>). The server will return a <spanx style="emph">headers</spanx> property but with just the requested headers in the object rather than all headers. If <spanx style="verb">"headers"</spanx> is requested, the server MUST ignore the individual header requests and just return all headers. If a requested header is not present in the message, it MUST not be present in the <spanx style="emph">headers</spanx> object. Header names are case-insensitive.</t>
</list>
</t>
<t>The response to <spanx style="emph">getMessages</spanx> is called <spanx style="emph">messages</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">state</spanx>: <spanx style="verb">String</spanx>
A string encoding the current state on the server. This string will change
if any messages change (that is, a new message arrives, a change is made to one of the mutable properties, or a message is deleted). It can be passed to <spanx style="emph">getMessageUpdates</spanx> to efficiently get the list of changes from the previous state.</t>
<t><spanx style="strong">list</spanx>: <spanx style="verb">Message[]</spanx>
An array of Message objects for the requested message ids. This may not be in the same order as the ids were in the request.</t>
<t><spanx style="strong">notFound</spanx>: <spanx style="verb">String[]|null</spanx>
An array of message ids requested which could not be found, or <spanx style="verb">null</spanx> if all
ids were found.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="emph">messages</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">requestTooLarge</spanx>: Returned if the number of <spanx style="emph">ids</spanx> requested by the client exceeds the maximum number the server is willing to process in a single method call.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if the request does not include one of the required arguments, or one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="emph">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t>Example request:
</t>

<figure align="center"><artwork align="center">
["getMessages", {
  "ids": [ "f123u456", "f123u457" ],
  "properties": [ "threadId", "mailboxIds", "from", "subject", "date" ]
}, "#1"]
</artwork></figure>
<t>and response:
</t>

<figure align="center"><artwork align="center">
["messages", {
  "state": "41234123231",
  "list": [
    {
      messageId: "f123u457",
      threadId: "ef1314a",
      mailboxIds: [ "f123" ],
      from: [{name: "Joe Bloggs", email: "joe@bloggs.com"}],
      subject: "Dinner on Thursday?",
      date: "2013-10-13T14:12:00Z"
    }
  ],
  notFound: [ "f123u456" ]
}, "#1"]
</artwork></figure>
</section>

<section anchor="getmessageupdates" title="getMessageUpdates">
<t>If a call to <spanx style="emph">getMessages</spanx> returns with a different <spanx style="emph">state</spanx> string in the response to a previous call, the state of the messages has changed on the server. For example, a new message may have been delivered, or an existing message may have changed mailboxes.
</t>
<t>The <spanx style="emph">getMessageUpdates</spanx> call allows a client to efficiently update the state of any cached messages to match the new state on the server. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If not given, defaults to the primary account.</t>
<t><spanx style="strong">sinceState</spanx>: <spanx style="verb">String</spanx>
The current state of the client. This is the string that was returned as the <spanx style="emph">state</spanx> argument in the <spanx style="emph">messages</spanx> response. The server will return the changes made since this state.</t>
<t><spanx style="strong">maxChanges</spanx>: <spanx style="verb">Number|null</spanx>
The maximum number of changed messages to return in the response. The server MAY choose to clamp this value to a particular maximum or set a maximum if none is given by the client. If supplied by the client, the value MUST be a positive integer greater than 0. If a value outside of this range is given, the server MUST reject the call with an <spanx style="verb">invalidArguments</spanx> error.</t>
<t><spanx style="strong">fetchRecords</spanx>: <spanx style="verb">Boolean|null</spanx>
If true, immediately after outputting a <spanx style="emph">messageUpdates</spanx> response, an implicit call will be made to <spanx style="emph">getMessages</spanx> with a list of all message ids in the <spanx style="emph">changed</spanx> argument of the response as the <spanx style="emph">ids</spanx> argument, and the <spanx style="emph">fetchRecordProperties</spanx> argument as the <spanx style="emph">properties</spanx> argument.</t>
<t><spanx style="strong">fetchRecordProperties</spanx>: <spanx style="verb">String[]|null</spanx>
The list of properties to fetch on any fetched messages. See <spanx style="emph">getMessages</spanx> for a full description.</t>
</list>
</t>
<t>The response to <spanx style="emph">getMessageUpdates</spanx> is called <spanx style="emph">messageUpdates</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String</spanx>
This is the <spanx style="emph">sinceState</spanx> argument echoed back; the state from which the server is returning changes.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
This is the state the client will be in after applying the set of changes to the old state.</t>
<t><spanx style="strong">hasMoreUpdates</spanx>: <spanx style="verb">Boolean</spanx>
If <spanx style="verb">true</spanx>, the client may call <spanx style="emph">getMessageUpdates</spanx> again with the <spanx style="emph">newState</spanx> returned to get further updates. If <spanx style="verb">false</spanx>, <spanx style="emph">newState</spanx> is the current server state.</t>
<t><spanx style="strong">changed</spanx>: <spanx style="verb">String[]</spanx>
An array of message ids for messages that have either been created or had their state change, and are not currently deleted.</t>
<t><spanx style="strong">removed</spanx>: <spanx style="verb">String[]</spanx>
An array of message ids for messages that have been deleted since the oldState.</t>
</list>
</t>
<t>If a <spanx style="emph">maxChanges</spanx> is supplied, or set automatically by the server, the server MUST ensure the number of ids returned across <spanx style="emph">changed</spanx> and <spanx style="emph">removed</spanx> does not exceed this limit. If there are more changes than this between the client's state and the current server state, the update returned SHOULD generate an update to take the client to an intermediate state, from which the client can continue to call <spanx style="emph">getMessageUpdates</spanx> until it is fully up to date. If it is unable to calculate an intermediate state, it MUST return a <spanx style="verb">cannotCalculateChanges</spanx> error response instead.
</t>
<t>If a message has been modified AND deleted since the oldState, the server SHOULD just return the id in the <spanx style="emph">removed</spanx> response, but MAY return it in the changed response as well. If a message has been created AND deleted since the oldState, the server SHOULD remove the message id from the response entirely, but MAY include it in the <spanx style="emph">removed</spanx> response, and (if in the <spanx style="emph">removed</spanx> response) MAY included it in the <spanx style="emph">changed</spanx> response as well.
</t>
<t>The following errors may be returned instead of the <spanx style="emph">messageUpdates</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if the request does not include one of the required arguments, or one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="emph">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t><spanx style="verb">cannotCalculateChanges</spanx>: Returned if the server cannot calculate the changes from the state string given by the client. Usually due to the client's state being too old, or the server being unable to produce an update to an intermediate state when there are too many updates. The client MUST invalidate its Message cache.
</t>
</section>

<section anchor="setmessages" title="setMessages">
<t>The <spanx style="emph">setMessages</spanx> method encompasses:
</t>
<t>
<list style="symbols">
<t>Creating a draft message</t>
<t>Sending a message</t>
<t>Changing the flags of a message (unread/flagged status)</t>
<t>Adding/removing a message to/from mailboxes (moving a message)</t>
<t>Deleting messages</t>
</list>
</t>
<t>It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If not given, defaults to the primary account.</t>
<t><spanx style="strong">ifInState</spanx>: <spanx style="verb">String|null</spanx>
This is a state string as returned by the <spanx style="emph">getMessages</spanx> method. If supplied, the string must match the current state, otherwise the method will be aborted and a <spanx style="verb">stateMismatch</spanx> error returned. If <spanx style="verb">null</spanx>, any changes will be applied to the current state.</t>
<t><spanx style="strong">create</spanx>: <spanx style="verb">String[Message]|null</spanx>
A map of <spanx style="emph">creation id</spanx> (an arbitrary string set by the client) to Message objects (see below for a detailed description).</t>
<t><spanx style="strong">update</spanx>: <spanx style="verb">String[Message]|null</spanx>
A map of message id to objects containing the properties to update for that Message.</t>
<t><spanx style="strong">destroy</spanx>: <spanx style="verb">String[]|null</spanx>
A list of ids for Message objects to permanently delete.</t>
</list>
</t>
<t>Each create, update or destroy is considered an atomic unit. It is permissible for the server to commit some of the changes but not others, however it is not permissible to only commit part of an update to a single record (e.g. update the <spanx style="emph">keywords</spanx> field but not the <spanx style="emph">mailboxIds</spanx> field, if both are supplied in the update object for a message).
</t>
<t>If a create, update or destroy is rejected, the appropriate error MUST be added to the notCreated/notUpdated/notDestroyed property of the response and the server MUST continue to the next create/update/destroy. It does not terminate the method.
</t>
<t>If an id given cannot be found, the update or destroy MUST be rejected with a <spanx style="verb">notFound</spanx> set error.
</t>

<section anchor="saving-a-draft" title="Saving a draft">
<t>Creating messages via the <spanx style="emph">setMessages</spanx> method is only for creating draft messages and sending them. For delivering/importing a complete <xref target="RFC5322"/> message, use the <spanx style="verb">importMessages</spanx> method.
</t>
<t>The properties of the Message object submitted for creation MUST conform to the following conditions:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">id</spanx>: This property MUST NOT be included. It is set by the server upon creation.</t>
<t><spanx style="strong">blobId</spanx>: This property MUST NOT be included. It is set by the server upon creation.</t>
<t><spanx style="strong">threadId</spanx>: This property MUST NOT be included. It is set by the server upon creation.</t>
<t><spanx style="strong">mailboxIds</spanx>: This property MUST be included. The value MUST include the id of either the mailbox with <spanx style="verb">role == "drafts"</spanx> (to save a draft) or the mailbox with <spanx style="verb">role == "outbox"</spanx> (to send the message). If this mailbox does not have <spanx style="verb">mustBeOnlyMailbox == true</spanx>, others may be included too.</t>
<t><spanx style="strong">keywords</spanx>: This property MUST be included. It MUST include the <spanx style="verb">$Draft</spanx> keyword and SHOULD also include <spanx style="verb">$Seen</spanx>.</t>
<t><spanx style="strong">hasAttachment</spanx>: This property MUST NOT be included. It is set by the server upon creation based on the attachments property.</t>
<t><spanx style="strong">headers</spanx>: Optional. The keys MUST only contain the characters a-z
(lower-case only), 0-9 and hyphens.</t>
<t><spanx style="strong">from</spanx>: Optional. Overrides a &quot;From&quot; in the <spanx style="emph">headers</spanx>.</t>
<t><spanx style="strong">to</spanx>: Optional. Overrides a &quot;To&quot; in the <spanx style="emph">headers</spanx>.</t>
<t><spanx style="strong">cc</spanx>: Optional. Overrides a &quot;Cc&quot; in the <spanx style="emph">headers</spanx>.</t>
<t><spanx style="strong">bcc</spanx>:  Optional. Overrides a &quot;Bcc&quot; in the <spanx style="emph">headers</spanx>.</t>
<t><spanx style="strong">replyTo</spanx>: Optional. Overrides a &quot;Reply-To&quot; in the <spanx style="emph">headers</spanx>.</t>
<t><spanx style="strong">subject</spanx>: Optional. Defaults to the empty string (<spanx style="verb">""</spanx>).</t>
<t><spanx style="strong">date</spanx>: Optional. If included, the server SHOULD wait until this time to send the message (once moved to the outbox mailbox). Until it is sent, the send may be cancelled by moving the message back out of the outbox mailbox. If the date is in the past, the message must be sent immediately. A client may find out if the server supports delayed sending by querying the server's <spanx style="emph">capabilities</spanx> object (see section 1).</t>
<t><spanx style="strong">size</spanx>: This MUST NOT be included. It is set by the server upon creation.</t>
<t><spanx style="strong">preview</spanx>: This MUST NOT be included. It is set by the server upon creation.</t>
<t><spanx style="strong">textBody</spanx>: Optional. If not supplied and an htmlBody is, the server SHOULD generate a text version for the message from the HTML body.</t>
<t><spanx style="strong">htmlBody</spanx>: Optional.</t>
<t><spanx style="strong">attachments</spanx>: Optional. An array of Attachment objects detailing all the attachments to the message. To add an attachment, the file must first be uploaded using the standard upload mechanism; this will give the client a blobId that may be used to identify the file. The <spanx style="verb">cid</spanx> property may be assigned by the client, and is solely used for matching up with <spanx style="verb">cid:&lt;id&gt;</spanx> links inside the <spanx style="verb">htmlBody</spanx>. The server MAY change the cids upon sending.</t>
</list>
</t>
<t>If any of the files specified in <spanx style="emph">attachments</spanx> cannot be found, the creation MUST be rejected with an <spanx style="verb">invalidProperties</spanx> error. An extra property SHOULD be included in the error object called <spanx style="verb">attachmentsNotFound</spanx>, of type <spanx style="verb">String[]</spanx>, which SHOULD be an array of the <spanx style="emph">blobId</spanx> of every attachment that could not be found on the server.
- <spanx style="strong">attachedMessages</spanx>: This MUST NOT be included.
</t>
<t>All optional properties default to <spanx style="verb">null</spanx> unless otherwise stated. Where included, properties MUST conform to the type given in the Message object definition.
</t>
<t>If any of the properties are invalid, the server MUST reject the create with an <spanx style="verb">invalidProperties</spanx> error. The Error object SHOULD contain a property called <spanx style="emph">properties</spanx> of type <spanx style="verb">String[]</spanx> that lists <spanx style="strong">all</spanx> the properties that were invalid. The object MAY also contain a <spanx style="emph">description</spanx> property of type <spanx style="verb">String</spanx> with a user-friendly description of the problems.
</t>
<t>Other than making sure it conforms to the correct type, the server MUST NOT attempt to validate from/to/cc/bcc when saved as a draft. This is to ensure messages can be saved at any point. Validation occurs when the user tries to send a message.
</t>
<t>If a draft cannot be saved due to the user reaching their maximum mail storage quota, the creation MUST be rejected with a <spanx style="verb">maxQuotaReached</spanx> error.
</t>
</section>

<section anchor="updating-messages" title="Updating messages">
<t>Messages are mainly immutable, so to update a draft the client must create a new message and delete the old one. This ensures that if the draft is also being edited elsewhere, the two will split into two different drafts to avoid data loss.
</t>
<t>Only the <spanx style="emph">mailboxIds</spanx> and <spanx style="emph">keywords</spanx> properties may be modified, and they are subject to the following constraints:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">mailboxIds</spanx>: The server MUST reject any attempt to add a message to the outbox that does not have the <spanx style="verb">$Draft</spanx> keyword with an <spanx style="verb">invalidProperties</spanx> error.</t>
<t><spanx style="strong">keywords</spanx>: The server MUST reject any attempt to add or remove the <spanx style="verb">$Draft</spanx> flag in an update with an <spanx style="verb">invalidProperties</spanx> error. The server MAY have a maximum number of keywords it supports; if the change would exceed this, it MUST be rejected with a <spanx style="verb">tooManyKeywords</spanx> error.</t>
</list>
</t>
<t>Note, a mailbox id may be a <spanx style="emph">creation id</spanx> (see <spanx style="verb">setFoos</spanx> for a description of how this works).
</t>
<t>If any of the properties in the update are invalid (immutable and different to the current server value, wrong type, invalid value for the property – like a mailbox id for non-existent mailbox), the server MUST reject the update with an <spanx style="verb">invalidProperties</spanx> error. The Error object SHOULD contain a property called <spanx style="emph">properties</spanx> of type <spanx style="verb">String[]</spanx> that lists <spanx style="strong">all</spanx> the properties that were invalid. The object MAY also contain a <spanx style="emph">description</spanx> property of type <spanx style="verb">String</spanx> with a user-friendly description of the problems.
</t>
<t>If the <spanx style="emph">id</spanx> given does not correspond to a Message in the given account, reject the update with a <spanx style="verb">notFound</spanx> error.
</t>
<t>To <spanx style="strong">delete a message</spanx> to trash, simply change the <spanx style="verb">mailboxIds</spanx> property so it is now in the mailbox with <spanx style="verb">role == "trash"</spanx>, and remove all other mailbox ids.
</t>
</section>

<section anchor="sending-messages" title="Sending messages">
<t>To send a message, either create a new message directly into the mailbox with <spanx style="verb">role == "outbox"</spanx> or move an existing draft into this mailbox. At this point the server will check that it has everything it needs for a valid message. In particular, that it has a valid &quot;From&quot; address (and the user has permission to use this From address), it has at least one address to send to, and all addresses in To/Cc/Bcc are valid email addresses. If it cannot send, it will reject the creation/update with an <spanx style="verb">invalidProperties</spanx> error. The Error object SHOULD contain a property called <spanx style="emph">properties</spanx> of type <spanx style="verb">String[]</spanx> that lists <spanx style="strong">all</spanx> the properties that were invalid. The object SHOULD also contain a <spanx style="emph">description</spanx> property of type <spanx style="verb">String</spanx> with a user-friendly description of the problems to present to the user.
</t>
<t>If the message is accepted, the server SHOULD <spanx style="strong">asynchronously</spanx> schedule the message to be sent <spanx style="strong">after</spanx> this method call is complete (note, this MAY occur before the next method in the same API request or after the whole API request is complete). This means that the <spanx style="verb">newState</spanx> string in the response represents a state where the message is still in the outbox.
</t>
<t>When the message is sent, the server MUST remove the message from the <spanx style="strong">outbox</spanx> and add it to the <spanx style="strong">sent</spanx> mailbox, unless the user has indicated another preference. The version added to the sent mailbox MAY be different (for example have extra headers added by the server), and so have a different id to the version that was in the outbox. If the message has an <spanx style="verb">In-Reply-To</spanx> header, the server SHOULD add the <spanx style="verb">$Answered</spanx> keyword to all messages with the coresponding <spanx style="verb">Message-Id</spanx> header at this point. If the message has an <spanx style="verb">X-Forwarded-Message-Id</spanx> header, the server SHOULD add the <spanx style="verb">$Forwarded</spanx> keyword to all messages with the coresponding <spanx style="verb">Message-Id</spanx> header at this point.
</t>
<t>The server is responsible for either reporting an error (normally a &quot;bounce&quot; email), or ensuring delivery of the message to the next hop.
</t>
</section>

<section anchor="cancelling-a-send" title="Cancelling a send">
<t>A message may be moved out of the <spanx style="strong">outbox</spanx> and back to the <spanx style="strong">drafts</spanx> mailbox using the standard update message mechanism, if it has not yet been sent at the time the method is called. This MUST cancel the queued send. If the message has already been sent then it will have been deleted from the outbox, so the update will fail with a standard <spanx style="verb">notFound</spanx> error.
</t>
</section>

<section anchor="destroying-messages" title="Destroying messages">
<t>If the <spanx style="emph">id</spanx> given does not correspond to a Message in the given account, the server MUST reject the destruction with a <spanx style="verb">notFound</spanx> error.
</t>
<t>Destroying a message removes it from all mailboxes to which it belonged.
</t>
<t>When emptying the trash, clients SHOULD NOT destroy messages which are also in a mailbox other than trash. For those messages, they should just remove the Trash mailbox from the message.
</t>
</section>

<section anchor="response-2" title="Response">
<t>The response to <spanx style="emph">setMessages</spanx> is called <spanx style="emph">messagesSet</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String|null</spanx>
The state string that would have been returned by <spanx style="emph">getMessages</spanx> before making the requested changes, or <spanx style="verb">null</spanx> if the server doesn't know what the previous state string was.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
The state string that will now be returned by <spanx style="emph">getMessages</spanx>.</t>
<t><spanx style="strong">created</spanx>: <spanx style="verb">String[Message]</spanx>
A map of the creation id to an object containing the <spanx style="emph">id</spanx>, <spanx style="emph">blobId</spanx>, <spanx style="emph">threadId</spanx>, and <spanx style="emph">size</spanx> properties for each successfully created Message.</t>
<t><spanx style="strong">updated</spanx>: <spanx style="verb">String[Message|null]</spanx>
The <spanx style="emph">keys</spanx> in this map are the ids of all messages that were successfully updated. If the server made any other changes to the record beyond those explicitly requested by the client, the <spanx style="emph">value</spanx> for the corresponding id in the map is an object containing the updated value of each property the <spanx style="strong">server changed</spanx>. Otherwise (if no properties changed on the server other than those explicitly updated by the client), the value is <spanx style="verb">null</spanx>.</t>
<t><spanx style="strong">destroyed</spanx>: <spanx style="verb">String[]</spanx>
A list of Message ids for Messages that were successfully destroyed.</t>
<t><spanx style="strong">notCreated</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of creation id to a SetError object for each Message that failed to be created. The possible errors are defined above.</t>
<t><spanx style="strong">notUpdated</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of Message id to a SetError object for each Message that failed to be updated. The possible errors are defined above.</t>
<t><spanx style="strong">notDestroyed</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of Message id to a SetError object for each Message that failed to be destroyed. The possible errors are defined above.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="emph">messagesSet</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">accountReadOnly</spanx>: Returned if the account has <spanx style="verb">isReadOnly == true</spanx>.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="emph">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t><spanx style="verb">stateMismatch</spanx>: Returned if an <spanx style="emph">ifInState</spanx> argument was supplied and it does not match the current state.
</t>
</section>
</section>

<section anchor="importmessages" title="importMessages">
<t>The <spanx style="emph">importMessages</spanx> method adds <xref target="RFC5322"/> messages to a user's set of messages. The messages must first be uploaded as a file using the standard upload mechanism. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If <spanx style="verb">null</spanx>, defaults to the primary account.</t>
<t><spanx style="strong">messages</spanx>: <spanx style="verb">String[MessageImport]</spanx>
A map of creation id (client specified) to MessageImport objects</t>
</list>
</t>
<t>An <spanx style="strong">MessageImport</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">blobId</spanx>: <spanx style="verb">String</spanx>
The id representing the raw <xref target="RFC5322"/> message (see the file upload section).</t>
<t><spanx style="strong">mailboxIds</spanx> <spanx style="verb">String[]</spanx>
The ids of the mailbox(es) to assign this message to.</t>
<t><spanx style="strong">keywords</spanx>: <spanx style="verb">String[Boolean]</spanx></t>
</list>
</t>
<t>Adding to the outbox will send the message, as described in the <spanx style="emph">setMessages</spanx> section. The <spanx style="verb">$Draft</spanx> keyword MUST also be included if the message is being imported to the outbox.
</t>
<t>The response to <spanx style="emph">importMessages</spanx> is called <spanx style="emph">messagesImported</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for this call.</t>
<t><spanx style="strong">created</spanx>: <spanx style="verb">String[Message]</spanx>
A map of the creation id to an object containing the <spanx style="emph">id</spanx>, <spanx style="emph">blobId</spanx>, <spanx style="emph">threadId</spanx> and <spanx style="emph">size</spanx> properties for each successfully imported Message.</t>
<t><spanx style="strong">notCreated</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of creation id to a SetError object for each Message that failed to be created. The possible errors are defined above.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="emph">messageImported</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">accountReadOnly</spanx>: Returned if the account has <spanx style="verb">isReadOnly == true</spanx>.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="verb">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t><spanx style="verb">notFound</spanx>: Returned if the URL given in the <spanx style="verb">file</spanx> argument does not correspond to an internal file.
</t>
<t><spanx style="verb">invalidMailboxes</spanx>: Returned if one of the mailbox ids cannot be found, or an invalid combination of mailbox ids is specified.
</t>
<t><spanx style="verb">maxQuotaReached</spanx>: Returned if the user has reached their mail quota so the message cannot be imported.
</t>
</section>

<section anchor="copymessages" title="copyMessages">
<t>The only way to move messages <spanx style="strong">between</spanx> two different accounts is to copy them using the <spanx style="emph">copyMessages</spanx> method, then once the copy has succeeded, delete the original. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">fromAccountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to copy messages from. If <spanx style="verb">null</spanx>, defaults to the primary account.</t>
<t><spanx style="strong">toAccountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to copy messages to. If <spanx style="verb">null</spanx>, defaults to the primary account.</t>
<t><spanx style="strong">messages</spanx>: <spanx style="verb">String[MessageCopy]</spanx>
A map of <spanx style="emph">creation id</spanx> to a MessageCopy object.</t>
</list>
</t>
<t>A <spanx style="strong">MessageCopy</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">messageId</spanx>: <spanx style="verb">String</spanx>
The id of the message to be copied in the &quot;from&quot; account.</t>
<t><spanx style="strong">mailboxIds</spanx>: <spanx style="verb">String[]</spanx>
The ids of the mailboxes (in the &quot;to&quot; account) to add the copied message to.</t>
<t><spanx style="strong">keywords</spanx>: <spanx style="verb">String[Boolean]</spanx>
The <spanx style="emph">keywords</spanx> property for the copy.</t>
</list>
</t>
<t>The &quot;from&quot; account may be the same as the &quot;to&quot; account to copy messages within an account.
</t>
<t>The response to <spanx style="emph">copyMessages</spanx> is called <spanx style="emph">messagesCopied</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">fromAccountId</spanx>: <spanx style="verb">String</spanx>
The id of the account messages were copied from.</t>
<t><spanx style="strong">toAccountId</spanx>: <spanx style="verb">String</spanx>
The id of the account messages were copied to.</t>
<t><spanx style="strong">created</spanx>: <spanx style="verb">String[Message]|null</spanx>
A map of the creation id to an object containing the <spanx style="emph">id</spanx>, <spanx style="emph">blobId</spanx>, <spanx style="emph">threadId</spanx> and <spanx style="emph">size</spanx> properties for each successfully copied Message.</t>
<t><spanx style="strong">notCreated</spanx>: <spanx style="verb">String[SetError]|null</spanx>
A map of creation id to a SetError object for each Message that failed to be copied, <spanx style="verb">null</spanx> if none.</t>
</list>
</t>
<t>The <spanx style="strong">SetError</spanx> may be one of the following types:
</t>
<t><spanx style="verb">notFound</spanx>: Returned if the messageId given can't be found.
</t>
<t><spanx style="verb">invalidMailboxes</spanx>: Returned if one of the mailbox ids cannot be found, or an invalid combination of mailbox ids is specified.
</t>
<t><spanx style="verb">maxQuotaReached</spanx>: Returned if the user has reached their mail quota so the message cannot be copied.
</t>
<t>The following errors may be returned instead of the <spanx style="emph">messagesCopied</spanx> response:
</t>
<t><spanx style="verb">fromAccountNotFound</spanx>: Returned if a <spanx style="emph">fromAccountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">toAccountNotFound</spanx>: Returned if a <spanx style="emph">toAccountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">fromAccountNoMail</spanx>: Returned if the <spanx style="emph">fromAccountId</spanx> given corresponds to a valid account, but does not contain any mail data.
</t>
<t><spanx style="verb">toAccountNoMail</spanx>: Returned if the <spanx style="emph">toAccountId</spanx> given corresponds to a valid account, but does not contain any mail data.
</t>
<t><spanx style="verb">accountReadOnly</spanx>: Returned if the &quot;to&quot; account has <spanx style="verb">isReadOnly == true</spanx>.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="verb">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
</section>
</section>

<section anchor="identities" title="Identities">
<t>A <spanx style="strong">Identity</spanx> object stores information about an email address (or domain) the user may send from. It has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">id</spanx>: <spanx style="verb">String</spanx>
The id of the identity. This property is immutable.</t>
<t><spanx style="strong">name</spanx>: <spanx style="verb">String</spanx>
The &quot;From&quot; <spanx style="emph">name</spanx> the client SHOULD use when creating a new message from this identity.</t>
<t><spanx style="strong">email</spanx>: <spanx style="verb">String</spanx>
The &quot;From&quot; email address the client MUST use when creating a new message from this identity. This property is immutable. The <spanx style="verb">email</spanx> property MAY alternatively be of the form <spanx style="verb">*@example.com</spanx>, in which case the client may use any valid email address ending in <spanx style="verb">@example.com</spanx>.</t>
<t><spanx style="strong">replyTo</spanx>: <spanx style="verb">String</spanx>
The Reply-To value the client SHOULD set when creating a new message from this identity.</t>
<t><spanx style="strong">bcc</spanx>: <spanx style="verb">String</spanx>
The Bcc value the client SHOULD set when creating a new message from this identity.</t>
<t><spanx style="strong">textSignature</spanx>: <spanx style="verb">String</spanx>
Signature the client SHOULD insert into new rich-text messages that will be sending from this identity. Clients MAY ignore this and/or combine this with a client-specific signature preference.</t>
<t><spanx style="strong">htmlSignature</spanx>: <spanx style="verb">String</spanx>
Signature the client SHOULD insert into new HTML messages that will be sending from this identity. This text MUST be an HTML snippet to be inserted into the <spanx style="verb">&lt;body&gt;&lt;/body&gt;</spanx> section of the new email. Clients MAY ignore this and/or combine this with a client-specific signature preference.</t>
<t><spanx style="strong">mayDeleteIdentity</spanx>: <spanx style="verb">Boolean</spanx>
Is the user allowed to delete this identity? Servers may wish to set this to
false for the user's username or other default address.</t>
</list>
</t>
<t>Multiple identities with the same email address MAY exist, to allow for different settings the user wants to pick between (for example with different names/signatures).
</t>

<section anchor="getidentities" title="getIdentities">
<t>Identities can either be fetched explicitly by id, or all of them at once. To fetch identities, make a call to <spanx style="verb">getIdentities</spanx>. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The Account to fetch the identities for. If <spanx style="verb">null</spanx>, the primary account is used.</t>
<t><spanx style="strong">ids</spanx>: <spanx style="verb">String[]|null</spanx>
The ids of the identities to fetch. If <spanx style="verb">null</spanx>, all identities in the account are be fetched.</t>
</list>
</t>
<t>The response to <spanx style="emph">getIdentities</spanx> is called <spanx style="emph">identities</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">state</spanx>: <spanx style="verb">String</spanx>
A string encoding the current state on the server. This string will change
if any identities change (that is, a new identity is created, a change is made to an existing identity, or an identity is deleted). It can be passed to <spanx style="emph">getIdentityUpdates</spanx> to efficiently get the list of changes from the previous state.</t>
<t><spanx style="strong">list</spanx>: <spanx style="verb">Identity[]</spanx>
An array of the Identity objects requested. This will be the <spanx style="strong">empty array</spanx> if the <spanx style="emph">ids</spanx> argument was the empty array, or contained only ids for identities that could not be found.</t>
<t><spanx style="strong">notFound</spanx>: <spanx style="verb">String[]|null</spanx>
This array contains the ids passed to the method for identities that do not exist, or <spanx style="verb">null</spanx> if all requested ids were found. It MUST be <spanx style="verb">null</spanx> if the <spanx style="emph">ids</spanx> argument in the call was <spanx style="verb">null</spanx>.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="emph">identities</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="verb">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
</section>

<section anchor="getidentityupdates" title="getIdentityUpdates">
<t>The <spanx style="emph">getIdentityUpdates</spanx> call allows a client to efficiently update the state of its cached identities to match the new state on the server. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If <spanx style="verb">null</spanx>, the primary account will be used.</t>
<t><spanx style="strong">sinceState</spanx>: <spanx style="verb">String</spanx>
The current state of the client. This is the string that was returned as the <spanx style="emph">state</spanx> argument in the <spanx style="emph">identities</spanx> response. The server will return the changes made since this state.</t>
<t><spanx style="strong">maxChanges</spanx>: <spanx style="verb">Number|null</spanx>
The maximum number of Identity ids to return in the response. The server MAY choose to clamp this value to a particular maximum or set a maximum if none is given by the client. If supplied by the client, the value MUST be a positive integer greater than 0. If a value outside of this range is given, the server MUST reject the call with an <spanx style="verb">invalidArguments</spanx> error.</t>
<t><spanx style="strong">fetchRecords</spanx>: <spanx style="verb">Boolean|null</spanx>
If <spanx style="verb">true</spanx>, immediately after outputting an <spanx style="emph">identityUpdates</spanx> response, an implicit call will be made to <spanx style="emph">getidentities</spanx> with the <spanx style="emph">changed</spanx> property of the response as the <spanx style="emph">ids</spanx> argument, and the <spanx style="emph">fetchRecordProperties</spanx> argument as the <spanx style="emph">properties</spanx> argument. If <spanx style="verb">false</spanx> or <spanx style="verb">null</spanx>, no implicit call is made.</t>
</list>
</t>
<t>The response to <spanx style="emph">getIdentityUpdates</spanx> is called <spanx style="emph">identityUpdates</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String</spanx>
This is the <spanx style="emph">sinceState</spanx> argument echoed back; the state from which the server is returning changes.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
This is the state the client will be in after applying the set of changes to the old state.</t>
<t><spanx style="strong">hasMoreUpdates</spanx>: <spanx style="verb">Boolean</spanx>
If <spanx style="verb">true</spanx>, the client may call <spanx style="emph">getIdentityUpdates</spanx> again with the <spanx style="emph">newState</spanx> returned to get further updates. If <spanx style="verb">false</spanx>, <spanx style="emph">newState</spanx> is the current server state.</t>
<t><spanx style="strong">changed</spanx>: <spanx style="verb">String[]</spanx>
An array of Identity ids where a property of the identity has changed between the old state and the new state, or the identity has been created, and the identity has not been destroyed.</t>
<t><spanx style="strong">removed</spanx>: <spanx style="verb">String[]</spanx>
An array of Identity ids for identities which have been destroyed since the old state.</t>
</list>
</t>
<t>If a <spanx style="emph">maxChanges</spanx> is supplied, or set automatically by the server, the server must try to limit the number of ids across <spanx style="emph">changed</spanx> and <spanx style="emph">removed</spanx> to the number given. If there are more changes than this between the client's state and the current server state, the update returned MUST take the client to an intermediate state, from which the client can continue to call <spanx style="emph">getIdentityUpdates</spanx> until it is fully up to date. The server MAY return more ids than the <spanx style="emph">maxChanges</spanx> total if this is required for it to be able to produce an update to an intermediate state, but it SHOULD try to keep it close to the maximum requested.
</t>
<t>If an identity has been modified AND deleted since the oldState, the server should just return the id in the <spanx style="emph">removed</spanx> array, but MAY return it in the <spanx style="emph">changed</spanx> array as well. If an identity has been created AND deleted since the oldState, the server SHOULD remove the identity id from the response entirely, but MAY include it in the <spanx style="emph">removed</spanx> array.
</t>
<t>The following errors may be returned instead of the <spanx style="verb">identityUpdates</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if the request does not include one of the required arguments, or one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="verb">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t><spanx style="verb">cannotCalculateChanges</spanx>: Returned if the server cannot calculate the changes from the state string given by the client. Usually due to the client's state being too old, or the server being unable to produce an update to an intermediate state when there are too many updates. The client MUST invalidate its Identity cache.
</t>
</section>

<section anchor="setidentities" title="setIdentities">
<t>Modifying the state of Identity objects on the server is done via the <spanx style="emph">setIdentities</spanx> method. This encompasses creating, updating and destroying Identity records.
</t>
<t>The <spanx style="emph">setIdentities</spanx> method takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If <spanx style="verb">null</spanx>, the primary account will be used.</t>
<t><spanx style="strong">ifInState</spanx>: <spanx style="verb">String|null</spanx>
This is a state string as returned by the <spanx style="emph">getIdentities</spanx> method. If supplied, the string must match the current state, otherwise the method MUST be aborted and a <spanx style="verb">stateMismatch</spanx> error returned. If <spanx style="verb">null</spanx>, any changes will be applied to the current state.</t>
<t><spanx style="strong">create</spanx>: <spanx style="verb">String[Identity]|null</spanx>
A map of <spanx style="emph">creation id</spanx> (an arbitrary string set by the client) to Identity objects (containing all properties except the id).</t>
<t><spanx style="strong">update</spanx>: <spanx style="verb">String[Identity]|null</spanx>
A map of id to Identity objects. The object may omit any property; only properties that have changed need be included.</t>
<t><spanx style="strong">destroy</spanx>: <spanx style="verb">String[]|null</spanx>
A list of ids for Identity objects to permanently delete.</t>
</list>
</t>
<t>Each create, update or destroy is considered an atomic unit. It is permissible for the server to commit some of the changes but not others, however it is not permissible to only commit part of an update to a single identity.
</t>
<t>If a create, update or destroy is rejected, the appropriate error MUST be added to the notCreated/notUpdated/notDestroyed property of the response and the server MUST continue to the next create/update/destroy. It does not terminate the method.
</t>
<t>A <spanx style="strong">create</spanx> MAY be rejected with one of the following errors:
</t>
<t>
<list style="symbols">
<t><spanx style="verb">maxQuotaReached</spanx>: Returned if the user has reached a server-defined limit on the number of identities.</t>
<t><spanx style="verb">emailNotPermitted</spanx>: Returned if the user tries to create an identity with an email address the user does not allow them to send from.</t>
</list>
</t>
<t>If the identity has <spanx style="verb">mayDeleteIdentity == false</spanx>, any attempt to destroy it MUST be rejected with a <spanx style="verb">forbidden</spanx> error.
</t>
<t>If an id given cannot be found, the update or destroy MUST be rejected with a <spanx style="verb">notFound</spanx> set error.
</t>
<t>The response to <spanx style="emph">setIdentities</spanx> is called <spanx style="emph">identitiesSet</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">oldState</spanx>: <spanx style="verb">String|null</spanx>
The state string that would have been returned by <spanx style="emph">getIdentities</spanx> before making the requested changes, or <spanx style="verb">null</spanx> if the server doesn't know what the previous state string was.</t>
<t><spanx style="strong">newState</spanx>: <spanx style="verb">String</spanx>
The state string that will now be returned by <spanx style="emph">getIdentities</spanx>.</t>
<t><spanx style="strong">created</spanx>: <spanx style="verb">String[Identity]</spanx>
A map of the creation id to an object containing the <spanx style="strong">id</spanx> property for all successfully created identities.</t>
<t><spanx style="strong">updated</spanx>: <spanx style="verb">String[Identity|null]</spanx>
The <spanx style="emph">keys</spanx> in this map are the ids of all identities that were successfully updated. If the server made any other changes to the record beyond those explicitly requested by the client, the <spanx style="emph">value</spanx> for the corresponding id in the map is an object containing the updated value of each property the <spanx style="strong">server changed</spanx>. Otherwise (if no properties changed on the server other than those explicitly updated by the client), the value is <spanx style="verb">null</spanx>.</t>
<t><spanx style="strong">destroyed</spanx>: <spanx style="verb">String[]</spanx>
A list of ids for identities that were successfully destroyed.</t>
<t><spanx style="strong">notCreated</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of creation id to a SetError object for each identity that failed to be created. The possible errors are defined in the description of the method for specific data types.</t>
<t><spanx style="strong">notUpdated</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of Identity id to a SetError object for each identity that failed to be updated. The possible errors are defined in the description of the method for specific data types.</t>
<t><spanx style="strong">notDestroyed</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of Identity id to a SetError object for each identity that failed to be destroyed. The possible errors are defined in the description of the method for specific data types.</t>
</list>
</t>
<t>A <spanx style="strong">SetError</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">type</spanx>: <spanx style="verb">String</spanx>
The type of error.</t>
<t><spanx style="strong">description</spanx>: <spanx style="verb">String|null</spanx>
A description of the error to display to the user.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="emph">identitiesSet</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">accountReadOnly</spanx>: Returned if the account has MailCapabilities with <spanx style="verb">isReadOnly == true</spanx>.
</t>
<t><spanx style="verb">requestTooLarge</spanx>: Returned if the total number of objects to create, update or destroy exceeds the maximum number the server is willing to process in a single method call.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="emph">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
<t><spanx style="verb">stateMismatch</spanx>: Returned if an <spanx style="emph">ifInState</spanx> argument was supplied and it does not match the current state.
</t>
</section>
</section>

<section anchor="searchsnippets" title="SearchSnippets">
<t>When doing a search on a <spanx style="verb">String</spanx> property, the client may wish to show the relevant section of the body that matches the search as a preview instead of the beginning of the message, and to highlight any matching terms in both this and the subject of the message. Search snippets represent this data.
</t>
<t>A <spanx style="strong">SearchSnippet</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">messageId</spanx>: <spanx style="verb">String</spanx>
The message id the snippet applies to.</t>
<t><spanx style="strong">subject</spanx>: <spanx style="verb">String|null</spanx>
If text from the filter matches the subject, this is the subject of the message HTML-escaped, with matching words/phrases wrapped in <spanx style="verb">&lt;mark&gt;&lt;/mark&gt;</spanx> tags. If it does not match, this is <spanx style="verb">null</spanx>.</t>
<t><spanx style="strong">preview</spanx>: <spanx style="verb">String|null</spanx>
If text from the filter matches the plain-text or HTML body, this is the relevant section of the body (converted to plain text if originally HTML), HTML-escaped, with matching words/phrases wrapped in <spanx style="verb">&lt;mark&gt;&lt;/mark&gt;</spanx> tags, up to 256 characters long. If it does not match, this is <spanx style="verb">null</spanx>.</t>
</list>
</t>
<t>It is server-defined what is a relevant section of the body for preview. If the server is unable to determine search snippets, it MUST return <spanx style="verb">null</spanx> for both the <spanx style="emph">subject</spanx> and <spanx style="emph">preview</spanx> properties.
</t>
<t>Note, unlike most data types, a SearchSnippet DOES NOT have a property called <spanx style="verb">id</spanx>.
</t>

<section anchor="getsearchsnippets" title="getSearchSnippets">
<t>To fetch search snippets, make a call to <spanx style="verb">getSearchSnippets</spanx>. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The id of the account to use for this call. If <spanx style="verb">null</spanx>, defaults to the primary account.</t>
<t><spanx style="strong">messageIds</spanx>: <spanx style="verb">String[]</spanx>
The list of ids of messages to fetch the snippets for.</t>
<t><spanx style="strong">filter</spanx>: <spanx style="verb">FilterCondition|FilterOperator|null</spanx>
The same filter as passed to getMessageList; see the description of this method for details.</t>
</list>
</t>
<t>The response to <spanx style="verb">getSearchSnippets</spanx> is called <spanx style="verb">searchSnippets</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">filter</spanx>: <spanx style="verb">FilterCondition|FilterOperator|null</spanx>
Echoed back from the call.</t>
<t><spanx style="strong">list</spanx>: <spanx style="verb">SearchSnippet[]</spanx>
An array of SearchSnippet objects for the requested message ids. This may not be in the same order as the ids that were in the request.</t>
<t><spanx style="strong">notFound</spanx>: <spanx style="verb">String[]|null</spanx>
An array of message ids requested which could not be found, or <spanx style="verb">null</spanx> if all
ids were found.</t>
</list>
</t>
<t>Since snippets are only based on immutable properties, there is no state string or update mechanism needed.
</t>
<t>The following errors may be returned instead of the <spanx style="emph">searchSnippets</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">requestTooLarge</spanx>: Returned if the number of <spanx style="emph">messageIds</spanx> requested by the client exceeds the maximum number the server is willing to process in a single method call.
</t>
<t><spanx style="verb">cannotDoFilter</spanx>: Returned if the server is unable to process the given <spanx style="emph">filter</spanx> for any reason.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if the request does not include one of the required arguments, or one of the arguments is of the wrong type, or otherwise invalid. A <spanx style="verb">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
</section>
</section>

<section anchor="vacation-response" title="Vacation Response">
<t>The <spanx style="strong">VacationResponse</spanx> object represents the state of vacation-response
related settings for an account. It has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">id</spanx>: <spanx style="verb">String</spanx>
The id of the object. This property is immutable. There is only ever one
vacation response object, and its id is <spanx style="verb">"singleton"</spanx>.</t>
<t><spanx style="strong">isEnabled</spanx> <spanx style="verb">Boolean</spanx>
Should a vacation response be sent if a message arrives between the <spanx style="emph">fromDate</spanx> and <spanx style="emph">toDate</spanx>?</t>
<t><spanx style="strong">fromDate</spanx>: <spanx style="verb">Date|null</spanx>
If <spanx style="emph">isEnabled</spanx> is <spanx style="verb">true</spanx>, the date/time after which messages that arrive should receive the user's vacation response, in UTC. If <spanx style="verb">null</spanx>, the vacation response is effective immediately.</t>
<t><spanx style="strong">toDate</spanx>: <spanx style="verb">Date|null</spanx>
If <spanx style="emph">isEnabled</spanx> is <spanx style="verb">true</spanx>, the date/time after which messages that arrive should no longer receive the user's vacation response, in UTC. If <spanx style="verb">null</spanx>, the vacation response is effective indefinitely.</t>
<t><spanx style="strong">subject</spanx>: <spanx style="verb">String|null</spanx>
The subject that will be used by the mail sent in response to messages when the vacation response is enabled. If null, an appropriate subject SHOULD be set by the server.</t>
<t><spanx style="strong">textBody</spanx>: <spanx style="verb">String|null</spanx>
The plain text part of the message to send in response to messages when the vacation response is enabled. If this is <spanx style="verb">null</spanx>, when the vacation message is sent a plain-text body part SHOULD be generated from the <spanx style="emph">htmlBody</spanx> but the server MAY choose to send the response as HTML only.</t>
<t><spanx style="strong">htmlBody</spanx>: <spanx style="verb">String|null</spanx>
The HTML message to send in response to messages when the vacation response is enabled. If this is <spanx style="verb">null</spanx>, when the vacation message is sent an HTML body part MAY be generated from the <spanx style="emph">textBody</spanx>, or the server MAY choose to send the response as plain-text only.</t>
</list>
</t>

<section anchor="getvacationresponse" title="getVacationResponse">
<t>There MUST only be exactly one VacationResponse object in an account. It MUST have the id <spanx style="verb">"singleton"</spanx>.
</t>
<t>To fetch the vacation response object, make a call to <spanx style="verb">getVacationResponse</spanx>. It takes the following argument:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The Account to get the vacation response for. If <spanx style="verb">null</spanx>, the primary account is used.</t>
</list>
</t>
<t>The response to <spanx style="emph">getVacationResponse</spanx> is called <spanx style="emph">vacationResponse</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String</spanx>
The id of the account used for the call.</t>
<t><spanx style="strong">list</spanx>: <spanx style="verb">VacationResponse[]</spanx>
An array containing the single VacationResponse object.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="emph">vacationResponse</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
</section>

<section anchor="setvacationresponse" title="setVacationResponse">
<t>Sets properties on the vacation response object. It takes the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">accountId</spanx>: <spanx style="verb">String|null</spanx>
The Account to set the vacation response for. If <spanx style="verb">null</spanx>, the primary account is used.</t>
<t><spanx style="strong">update</spanx>: <spanx style="verb">String[VacationResponse]|null</spanx>
A map of id (&quot;singleton&quot;) to the VacationResponse object with new values for the properties you wish to change. The object may omit any property; only properties that have changed need be included.</t>
</list>
</t>
<t>If any of the properties in the update are invalid (immutable and different to the current server value, wrong type), the server MUST reject the update with a SetError of type <spanx style="verb">invalidProperties</spanx>. The SetError object SHOULD contain a property called <spanx style="emph">properties</spanx> of type <spanx style="verb">String[]</spanx> that lists <spanx style="strong">all</spanx> the properties that were invalid. The object MAY also contain a <spanx style="emph">description</spanx> property of type <spanx style="verb">String</spanx> with a user-friendly description of the problems.
</t>
<t>The response is called <spanx style="emph">vacationResponseSet</spanx>. It has the following arguments:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">updated</spanx>: <spanx style="verb">String[]</spanx></t>
<t><spanx style="strong">updated</spanx>: <spanx style="verb">String[VacationResponse|null]</spanx>
If successfully updated, this map will have a sole key of &quot;singleton&quot;. The <spanx style="emph">value</spanx> in the map is an object containing the updated value of each property the <spanx style="strong">server changed</spanx> on the record, if any. Otherwise (if no properties changed on the server other than those explicitly updated by the client), the value is <spanx style="verb">null</spanx>.</t>
<t><spanx style="strong">notUpdated</spanx>: <spanx style="verb">String[SetError]</spanx>
A map of id (&quot;singleton&quot;) to a SetError object if the update failed.</t>
</list>
</t>
<t>A <spanx style="strong">SetError</spanx> object has the following properties:
</t>
<t>
<list style="symbols">
<t><spanx style="strong">type</spanx>: <spanx style="verb">String</spanx>
The type of error.</t>
<t><spanx style="strong">description</spanx>: <spanx style="verb">String|null</spanx>
A description of the error to display to the user.</t>
</list>
</t>
<t>The following errors may be returned instead of the <spanx style="emph">vacationResponseSet</spanx> response:
</t>
<t><spanx style="verb">accountNotFound</spanx>: Returned if an <spanx style="emph">accountId</spanx> was explicitly included with the request, but it does not correspond to a valid account.
</t>
<t><spanx style="verb">accountNotSupportedByMethod</spanx>: Returned if the <spanx style="emph">accountId</spanx> given corresponds to a valid account, but the account does not support this data type.
</t>
<t><spanx style="verb">invalidArguments</spanx>: Returned if one of the arguments is of the wrong type, or otherwise invalid (including using an id other than <spanx style="verb">"singleton"</spanx>). A <spanx style="verb">description</spanx> property MAY be present on the response object to help debug with an explanation of what the problem was.
</t>
</section>
</section>

</middle>
<back>
<references title="Normative References">
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.2119.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.3629.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5322.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.5788.xml"?>
<?rfc include="http://xml2rfc.ietf.org/public/rfc/bibxml/reference.RFC.7159.xml"?>
</references>

</back>
</rfc>
