Mailboxes, messages and flags
Stalwart keeps one copy of each account’s mail, and every access protocol reads and changes that copy. The protocols were designed at different times, however, and each has its own picture of what a mailbox and a message are. JMAP lets a message be in several mailboxes at once, IMAP knows only folders containing messages, and POP3 knows only a list of messages in the inbox. This page describes the model the server keeps and how each protocol maps onto it, which explains most of the differences users notice when they use several clients on one account.
Mailboxes
Section titled “Mailboxes”A mailbox is a folder with a name, an optional parent and an optional role. The role marks the folder that serves a purpose, such as the inbox, sent mail or the trash, so that clients find it without relying on its name. Each account starts with the folders described in Default folders, and the depth of the hierarchy and the length of names are bounded as described in Message and mailbox limits.
The protocols present the same folders in different ways. JMAP returns each mailbox as an object with its role, parentId and counts. IMAP returns folder paths separated by /, with the role as a special-use attribute. POP3 sees only the inbox. Mailboxes that other accounts share with the user appear as separate accounts in JMAP and under a virtual folder in IMAP, as described in Shared mailboxes.
Messages in several mailboxes
Section titled “Messages in several mailboxes”In Stalwart a message is a single object that belongs to one or more mailboxes. JMAP exposes this directly: the mailboxIds property of a message lists every mailbox it is in, and a client can add or remove mailboxes without copying the message. The number of mailboxes a message may belong to is bounded by maxMailboxesPerEmail.
IMAP has no such notion, so a message filed in three mailboxes appears in each of the three IMAP folders, with a UID of its own in each. The copies are not independent: they share their flags, so marking the message as read in one folder marks it as read in the others. An IMAP COPY into another folder of the same account adds a mailbox to the same message rather than storing the message twice; a copy into a mailbox that another account shares with the user stores a new message in that account.
Flags and keywords
Section titled “Flags and keywords”IMAP flags and JMAP keywords are the same values, stored once per message. The system flags of IMAP correspond to JMAP keywords as follows:
| IMAP flag | JMAP keyword |
|---|---|
\Seen | $seen |
\Flagged | $flagged |
\Answered | $answered |
\Draft | $draft |
\Deleted | $deleted |
Other keywords, such as $Forwarded, $Junk and $NotJunk, and keywords that clients invent, are stored under the same name in both protocols. Their number and length per message are bounded as described in Mailboxes and flags per message. POP3 neither reads nor changes flags: retrieving a message over POP3 does not mark it as read.
Two keywords have an effect beyond the message itself. When the spam classifier is in use, setting $junk on a message, or moving it into the Junk folder, trains the classifier with the message as spam, and setting $notjunk, removing $junk or moving a message out of the Junk folder trains it as ham. Training by users lists the commands of each protocol that do this.
Message UIDs
Section titled “Message UIDs”IMAP identifies each message in a folder by a UID, a number that increases with every message added to the folder. Clients cache messages by UID, and they trust the cache for as long as the folder’s UIDVALIDITY value stays the same. Stalwart assigns a random UIDVALIDITY to each mailbox when it is created, and assigns UIDs per mailbox, so a message filed in several folders has a different UID in each. POP3 builds its message identifiers from the inbox’s UIDVALIDITY and UIDs, as described in Message identifiers.
The UIDs of an account can be reassigned by the administrator. The reassignment runs as an AccountMaintenance task with the operation recalculateImapUid, and the following command runs it at once for the account with the identifier c:
stalwart-cli create Task/AccountMaintenance \ --field accountId=c \ --field maintenanceType=recalculateImapUid \ --field 'status={"@type":"Pending","due":"2026-01-01T00:00:00Z"}'The task gives every mailbox of the account a new UIDVALIDITY and every message new UIDs. Every IMAP client of the account then discards its cache and downloads the mailboxes again, and POP3 clients that leave mail on the server download the whole inbox again. JMAP clients are not affected, since JMAP identifies messages by identifiers that do not depend on UIDs.
Threads
Section titled “Threads”The server groups messages into threads when they arrive, and every protocol sees the same threads: JMAP through the threadId property and the Thread object, and IMAP through the THREAD command and the THREADID of OBJECTID+.
A new message joins an existing thread when two conditions hold. First, it must share a message identifier with a message already in the thread: its Message-ID, In-Reply-To, References or Resent-Message-ID header names a message of the thread, or is named by one. Second, it must have the same base subject, the subject with prefixes such as Re: and Fwd: and mailing list tags removed. A reply whose subject was changed therefore starts a new thread, even though its References header links it to the old one, and two unrelated messages with the same subject stay apart because they share no identifier. A message without any of these headers always starts a thread of its own.
A message can link threads that were separate until it arrived, for example a reply that references messages from two threads with the same subject. The server then merges the threads into one, in a background task shortly after the message is stored.
Deleting messages
Section titled “Deleting messages”Removing a message from the server is a different operation in each protocol, and the differences matter when one user mixes clients:
- IMAP marks a message with
\Deleted, andEXPUNGEorCLOSEremoves the marked messages from the selected folder. A message that is also filed in other folders is removed from this folder only, and loses its\Deletedflag everywhere; a message filed in this folder only is destroyed. - JMAP removes a message from a mailbox by updating its
mailboxIds, and destroys it withEmail/set, which removes it from every mailbox at once. - POP3 destroys every message marked with
DELEwhen the session ends withQUIT, from every folder it was filed in, as described in Deleting messages.
None of these moves a message to the Trash folder. What a user sees as “delete” in a mail client is normally a move to Trash performed by the client, and the message stays there until the user empties the folder or the server’s auto-expunge removes it. A destroyed message is credited to the account’s quota at once. It is final unless the deleted-item archive of the Enterprise edition is enabled, in which case it can be recovered for the configured period.
Change history
Section titled “Change history”Clients synchronise incrementally: instead of downloading a mailbox again, they ask the server what changed since a state they saw before. JMAP clients use the /changes methods, IMAP clients use CONDSTORE and QRESYNC, and both are answered from a change history the server keeps per account. Every change is recorded there, whichever protocol made it, so a message moved over IMAP is reported to a JMAP client as a change to its mailboxIds. The length of the history, and what happens to a client that has been away for longer than it reaches, are described in Change history.