start static method

Future<MailInbox> start({
  1. required void onMail(
    1. InboxMail mail
    ),
  2. int port = 0,
  3. InternetAddress? address,
  4. ({String host, int port})? relay,
  5. void onRelayError(
    1. Object error
    )?,
})

Listens on port (0 picks a free one) on the loopback address unless address says otherwise, and calls onMail with each mail received.

onMail is called once the client has been told the mail was accepted. If it throws, that mail is not reported, but the client, the relay and every later mail are unaffected: a handler that cares about its errors catches them itself.

With a relay, every mail is forwarded to that SMTP server in the background, one at a time in the order they arrived, with the envelope it came with. A mail the relay could not take is reported to onRelayError as a MailRelayException, or dropped silently without one; either way the client that sent it was already told it was accepted.

Implementation

static Future<MailInbox> start({
  required void Function(InboxMail mail) onMail,
  int port = 0,
  InternetAddress? address,
  ({String host, int port})? relay,
  void Function(Object error)? onRelayError,
}) async {
  var server = await ServerSocket.bind(
    address ?? InternetAddress.loopbackIPv4,
    port,
  );
  var inbox = MailInbox._(server, onMail, relay, onRelayError);
  inbox._subscription = server.listen(inbox._accept);
  return inbox;
}