io-email 0.1.0

Email client library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Multi-protocol std-blocking email client.
//!
//! [`EmailClientStd`] is a thin container holding one optional per-protocol
//! client per supported backend. The shared-API surface (`list_mailboxes`,
//! `list_envelopes`, `get_message`, …) lives on each per-protocol client; this
//! struct gives callers a single typed bag to pass around the backends they
//! care about, plus a dispatch layer that picks the highest-priority registered
//! backend.
//!
//! Two construction paths:
//!
//! - `with_<protocol>(client)`: register a client built externally (typically
//!   via the per-protocol `new` or `connect`).
//! - `connect_<protocol>(…)`: TLS-gated convenience that opens the connection
//!   through the per-protocol `connect` and fills the slot in one shot.
//!
//! ## Dispatch priority
//!
//! Reads / mutations on storage backends: `Maildir → M2dir → JMAP → IMAP`
//! (local before network, cheap before expensive).
//!
//! Sending: `JMAP → SMTP`.
//!
//! When no registered backend supports the op, the dispatch returns
//! [`EmailClientStdError::NoBackendRegistered`].

use core::sync::atomic::AtomicBool;

use alloc::{string::String, sync::Arc, vec::Vec};

use std::sync::mpsc::Sender;

use thiserror::Error;

#[cfg(feature = "imap")]
use crate::imap::client::{ImapClientError, ImapClientStd};
#[cfg(feature = "jmap")]
use crate::jmap::client::{JmapClientError, JmapClientStd};
#[cfg(feature = "m2dir")]
use crate::m2dir::client::{M2dirClient, M2dirClientError};
#[cfg(feature = "maildir")]
use crate::maildir::client::{MaildirClient, MaildirClientError};
#[cfg(feature = "search")]
use crate::search::query::SearchEmailsQuery;
#[cfg(feature = "smtp")]
use crate::smtp::client::{SmtpClientError, SmtpClientStd};
use crate::{
    envelope::event::WatchEvent,
    envelope::types::{Envelope, EnvelopeDiff},
    flag::types::{Flag, FlagOp},
    mailbox::types::{Mailbox, MailboxDiff},
};

#[cfg(feature = "imap")]
use io_imap::types::core::{IString, NString};
#[cfg(feature = "smtp")]
#[cfg(any(
    feature = "rustls-ring",
    feature = "rustls-aws",
    feature = "native-tls"
))]
use io_smtp::rfc5321::types::ehlo_domain::EhloDomain;
#[cfg(feature = "imap")]
#[cfg(any(
    feature = "rustls-ring",
    feature = "rustls-aws",
    feature = "native-tls"
))]
use pimalaya_stream::sasl::Sasl as ImapSasl;
#[cfg(feature = "jmap")]
#[cfg(any(
    feature = "rustls-ring",
    feature = "rustls-aws",
    feature = "native-tls"
))]
use secrecy::SecretString;
#[cfg(any(feature = "imap", feature = "jmap", feature = "smtp"))]
#[cfg(any(
    feature = "rustls-ring",
    feature = "rustls-aws",
    feature = "native-tls"
))]
use {pimalaya_stream::tls::Tls, url::Url};

/// Errors surfaced by [`EmailClientStd`].
///
/// Each variant flattens the per-protocol client's error type via
/// `#[from]` so the matching `?` operator works on the shared client.
#[derive(Debug, Error)]
pub enum EmailClientStdError {
    #[cfg(feature = "imap")]
    #[error(transparent)]
    Imap(#[from] ImapClientError),
    #[cfg(feature = "jmap")]
    #[error(transparent)]
    Jmap(#[from] JmapClientError),
    #[cfg(feature = "smtp")]
    #[error(transparent)]
    Smtp(#[from] SmtpClientError),
    #[cfg(feature = "maildir")]
    #[error(transparent)]
    Maildir(#[from] MaildirClientError),
    #[cfg(feature = "m2dir")]
    #[error(transparent)]
    M2dir(#[from] M2dirClientError),
    #[error("No backend supporting this operation is registered")]
    NoBackendRegistered,
    #[error("Registered backend does not support this operation")]
    UnsupportedOperation,
}

/// Std-blocking multi-protocol email client.
///
/// Each slot holds an optional per-protocol client. Empty by default;
/// register backends through the `with_<protocol>` builders or the
/// `connect_<protocol>` convenience helpers. Slots are `pub` so
/// callers can read the registered client back out (e.g. to tweak its
/// pub knobs after construction).
#[derive(Default)]
pub struct EmailClientStd {
    #[cfg(feature = "imap")]
    pub imap: Option<ImapClientStd>,
    #[cfg(feature = "jmap")]
    pub jmap: Option<JmapClientStd>,
    #[cfg(feature = "smtp")]
    pub smtp: Option<SmtpClientStd>,
    #[cfg(feature = "maildir")]
    pub maildir: Option<MaildirClient>,
    #[cfg(feature = "m2dir")]
    pub m2dir: Option<M2dirClient>,
}

impl EmailClientStd {
    /// Builds an empty client with no backend registered.
    pub fn new() -> Self {
        Self::default()
    }

    /// Registers the IMAP backend.
    #[cfg(feature = "imap")]
    pub fn with_imap(mut self, client: ImapClientStd) -> Self {
        self.imap = Some(client);
        self
    }

    /// Registers the JMAP backend.
    #[cfg(feature = "jmap")]
    pub fn with_jmap(mut self, client: JmapClientStd) -> Self {
        self.jmap = Some(client);
        self
    }

    /// Registers the SMTP backend.
    #[cfg(feature = "smtp")]
    pub fn with_smtp(mut self, client: SmtpClientStd) -> Self {
        self.smtp = Some(client);
        self
    }

    /// Registers the Maildir backend.
    #[cfg(feature = "maildir")]
    pub fn with_maildir(mut self, client: MaildirClient) -> Self {
        self.maildir = Some(client);
        self
    }

    /// Registers the m2dir backend.
    #[cfg(feature = "m2dir")]
    pub fn with_m2dir(mut self, client: M2dirClient) -> Self {
        self.m2dir = Some(client);
        self
    }

    /// Opens an IMAP connection via [`ImapClientStd::connect`] and
    /// registers the resulting client. See [`ImapClientStd::connect`]
    /// for the `auto_id` semantics.
    #[cfg(feature = "imap")]
    #[cfg(any(
        feature = "rustls-ring",
        feature = "rustls-aws",
        feature = "native-tls"
    ))]
    pub fn connect_imap(
        self,
        url: &Url,
        tls: &Tls,
        starttls: bool,
        sasl: Option<impl Into<ImapSasl>>,
        auto_id: Option<Vec<(IString<'static>, NString<'static>)>>,
    ) -> Result<Self, EmailClientStdError> {
        Ok(self.with_imap(ImapClientStd::connect(url, tls, starttls, sasl, auto_id)?))
    }

    /// Opens a JMAP connection via [`JmapClientStd::connect`] and
    /// registers the resulting client (session already discovered).
    #[cfg(feature = "jmap")]
    #[cfg(any(
        feature = "rustls-ring",
        feature = "rustls-aws",
        feature = "native-tls"
    ))]
    pub fn connect_jmap(
        self,
        url: &Url,
        tls: &Tls,
        http_auth: SecretString,
    ) -> Result<Self, EmailClientStdError> {
        Ok(self.with_jmap(JmapClientStd::connect(url, tls, http_auth)?))
    }

    /// Opens an SMTP connection via [`SmtpClientStd::connect`] and
    /// registers the resulting client.
    #[cfg(feature = "smtp")]
    #[cfg(any(
        feature = "rustls-ring",
        feature = "rustls-aws",
        feature = "native-tls"
    ))]
    pub fn connect_smtp(
        self,
        url: &Url,
        tls: &Tls,
        starttls: bool,
        domain: EhloDomain<'_>,
        sasl: Option<impl Into<pimalaya_stream::sasl::Sasl>>,
    ) -> Result<Self, EmailClientStdError> {
        Ok(self.with_smtp(SmtpClientStd::connect(url, tls, starttls, domain, sasl)?))
    }

    /// Pings every registered network backend (IMAP, SMTP) to reset the
    /// server's inactivity timer on long-idle sessions. Storage backends
    /// (Maildir, M2dir) and JMAP (HTTP, stateless) have nothing to keep alive
    /// and are skipped. Returns the first error encountered, or `Ok(())` when
    /// every registered network backend acknowledged the NOOP.
    pub fn ping(&mut self) -> Result<(), EmailClientStdError> {
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            c.ping()?;
        }
        #[cfg(feature = "smtp")]
        if let Some(c) = self.smtp.as_mut() {
            c.ping()?;
        }
        Ok(())
    }

    // ---- Shared-API dispatch (storage: Maildir → M2dir → JMAP → IMAP) ----

    /// Lists every visible mailbox via the highest-priority registered storage
    /// backend.
    pub fn list_mailboxes(
        &mut self,
        with_counts: bool,
    ) -> Result<Vec<Mailbox>, EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.list_mailboxes(with_counts)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.list_mailboxes(with_counts)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.list_mailboxes(with_counts)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.list_mailboxes(with_counts)?);
        }
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Lists envelopes from `mailbox`. `with_attachment` is honoured by IMAP /
    /// Maildir / M2dir; JMAP returns the attachment flag inline and ignores the
    /// parameter.
    pub fn list_envelopes(
        &mut self,
        mailbox: &str,
        page: Option<u32>,
        page_size: Option<u32>,
        with_attachment: bool,
    ) -> Result<Vec<Envelope>, EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.list_envelopes(mailbox, page, page_size, with_attachment)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.list_envelopes(mailbox, page, page_size, with_attachment)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.list_envelopes(mailbox, page, page_size)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.list_envelopes(mailbox, page, page_size, with_attachment)?);
        }
        let _ = (mailbox, page, page_size, with_attachment);
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Searches envelopes against the shared query.
    #[cfg(feature = "search")]
    pub fn search_envelopes(
        &mut self,
        mailbox: &str,
        query: Option<&SearchEmailsQuery>,
        page: Option<u32>,
        page_size: Option<u32>,
        with_attachment: bool,
    ) -> Result<Vec<Envelope>, EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.search_envelopes(mailbox, query, page, page_size, with_attachment)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.search_envelopes(mailbox, query, page, page_size, with_attachment)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.search_envelopes(mailbox, query, page, page_size)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.search_envelopes(mailbox, query, page, page_size, with_attachment)?);
        }
        let _ = (mailbox, query, page, page_size, with_attachment);
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Adds, sets or removes flags on `ids`.
    pub fn store_flags(
        &mut self,
        mailbox: &str,
        ids: &[&str],
        flags: &[Flag],
        op: FlagOp,
    ) -> Result<(), EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.store_flags(mailbox, ids, flags, op)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.store_flags(mailbox, ids, flags, op)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.store_flags(mailbox, ids, flags, op)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.store_flags(mailbox, ids, flags, op)?);
        }
        let _ = (mailbox, ids, flags, op);
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Fetches one message's raw RFC 5322 bytes.
    pub fn get_message(&mut self, mailbox: &str, id: &str) -> Result<Vec<u8>, EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.get_message(mailbox, id)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.get_message(mailbox, id)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.get_message(mailbox, id)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.get_message(mailbox, id)?);
        }
        let _ = (mailbox, id);
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Adds `raw` to `mailbox` with the given flags. Returns the
    /// newly-assigned id.
    pub fn add_message(
        &mut self,
        mailbox: &str,
        flags: &[Flag],
        raw: Vec<u8>,
    ) -> Result<String, EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.add_message(mailbox, flags, raw)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.add_message(mailbox, flags, raw)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.add_message(mailbox, flags, raw)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.add_message(mailbox, flags, raw)?);
        }
        let _ = (mailbox, flags, raw);
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Creates `name` as a new mailbox.
    pub fn create_mailbox(&mut self, name: &str) -> Result<(), EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.create_mailbox(name)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.create_mailbox(name)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.create_mailbox(name)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.create_mailbox(name)?);
        }
        let _ = name;
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Deletes mailbox `name`.
    pub fn delete_mailbox(&mut self, name: &str) -> Result<(), EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.delete_mailbox(name)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.delete_mailbox(name)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.delete_mailbox(name)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.delete_mailbox(name)?);
        }
        let _ = name;
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Deletes one message permanently.
    pub fn delete_message(&mut self, mailbox: &str, id: &str) -> Result<(), EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.delete_message(mailbox, id)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.delete_message(mailbox, id)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.delete_message(mailbox, id)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.delete_message(mailbox, id)?);
        }
        let _ = (mailbox, id);
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Copies `ids` from `from` to `to`.
    pub fn copy_messages(
        &mut self,
        from: &str,
        to: &str,
        ids: &[&str],
    ) -> Result<(), EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.copy_messages(from, to, ids)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.copy_messages(from, to, ids)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.copy_messages(from, to, ids)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.copy_messages(from, to, ids)?);
        }
        let _ = (from, to, ids);
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Moves `ids` from `from` to `to`.
    pub fn move_messages(
        &mut self,
        from: &str,
        to: &str,
        ids: &[&str],
    ) -> Result<(), EmailClientStdError> {
        #[cfg(feature = "maildir")]
        if let Some(c) = &self.maildir {
            return Ok(c.move_messages(from, to, ids)?);
        }
        #[cfg(feature = "m2dir")]
        if let Some(c) = &self.m2dir {
            return Ok(c.move_messages(from, to, ids)?);
        }
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.move_messages(from, to, ids)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.move_messages(from, to, ids)?);
        }
        let _ = (from, to, ids);
        Err(EmailClientStdError::NoBackendRegistered)
    }

    /// Surfaces a pre-diffed envelope delta against the opaque
    /// per-backend `state` checkpoint, when the registered backend
    /// supports it.
    ///
    /// `state` is the blob returned by a previous successful call (or
    /// `None` on first sync); the caller stores the returned checkpoint
    /// for next time. Returns [`EnvelopeDiff::FullListRequired`] when
    /// the backend cannot produce an incremental view (capability
    /// missing, state invalidated, server bumped UIDVALIDITY).
    ///
    /// Currently routed to IMAP (QRESYNC / CONDSTORE) and JMAP
    /// (`Email/changes`); Maildir, m2dir and SMTP fall through to
    /// [`EmailClientStdError::UnsupportedOperation`].
    pub fn diff_envelopes(
        &mut self,
        mailbox: &str,
        state: Option<&[u8]>,
    ) -> Result<EnvelopeDiff, EmailClientStdError> {
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.diff_envelopes(mailbox, state)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.diff_envelopes(mailbox, state)?);
        }
        let _ = (mailbox, state);
        Err(EmailClientStdError::UnsupportedOperation)
    }

    /// Probes whether the mailbox set has changed since `state`. JMAP uses
    /// `Mailbox/changes` for a constant-cost "anything changed?"  answer;
    /// backends without an account-global mailbox state token (IMAP, Maildir,
    /// m2dir) fall through to [`EmailClientStdError::UnsupportedOperation`] so
    /// the caller can drop to a normal [`Self::list_mailboxes`].
    pub fn diff_mailboxes(
        &mut self,
        state: Option<&[u8]>,
    ) -> Result<MailboxDiff, EmailClientStdError> {
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.diff_mailboxes(state)?);
        }
        let _ = state;
        Err(EmailClientStdError::UnsupportedOperation)
    }

    /// Watches `mailbox` for envelope-level deltas, forwarding events
    /// through `tx`. Priority: JMAP → IMAP (no filesystem watch yet).
    #[cfg(any(feature = "imap", feature = "jmap"))]
    pub fn watch_mailbox(
        &mut self,
        mailbox: &str,
        shutdown: Arc<AtomicBool>,
        tx: Sender<WatchEvent>,
    ) -> Result<(), EmailClientStdError> {
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.watch_mailbox(mailbox, shutdown, tx)?);
        }
        #[cfg(feature = "imap")]
        if let Some(c) = self.imap.as_mut() {
            return Ok(c.watch_mailbox(mailbox, shutdown, tx)?);
        }
        let _ = (mailbox, shutdown, tx);
        Err(EmailClientStdError::NoBackendRegistered)
    }

    // ---- Sending (JMAP → SMTP) -------------------------------------

    /// Sends a raw RFC 5322 message. JMAP routes via `EmailSubmission/set` when
    /// registered; otherwise SMTP runs the RFC 5321 mail transaction.
    #[cfg(any(feature = "jmap", feature = "smtp"))]
    pub fn send_message(&mut self, raw: Vec<u8>) -> Result<(), EmailClientStdError> {
        #[cfg(feature = "jmap")]
        if let Some(c) = self.jmap.as_mut() {
            return Ok(c.send_message(raw)?);
        }
        #[cfg(feature = "smtp")]
        if let Some(c) = self.smtp.as_mut() {
            return Ok(c.send_message(raw)?);
        }
        let _ = raw;
        Err(EmailClientStdError::NoBackendRegistered)
    }
}