Skip to content

Expose the result of Mailer send-time rehearsal #709

Closed

Description

@bbottema

Preparation after shutdown — accepted 2 November 2026

Rehearsal currently reads its conversion context from the sending Session. Completed shutdown removes that context; reusing the Session for another Mailer can make the old Mailer's rehearsal consult the new owner's size policy.

mailer.close();
MailRehearsal preview = mailer.rehearse(email);
mailer.validate(email);

Keep rehearsal and validation usable after sending has closed. Both full and base preparation, including exact EML, continue through the original Mailer's rules. New and already-running preparations must not hold up sending-resource shutdown. Preserve an independently owned preparation context so another Mailer reusing the sending Session cannot change the old Mailer's previews.

While the Mailer is open, preserve the existing authority of effective supplied Session settings. At shutdown, retain a stable preparation view for calls made during or after closing. Capture configuration metadata without reading attachments, processing keys or opening transports. Previously accepted preparations retain the context captured for their call. Keep public getSession() and the sending Session's identity and ownership unchanged.

Preserve ordinary governance, locks, validation failures, security/size modes, exact-source bytes, caller-owned content and defensive returned snapshots. Rehearsal stays synchronous and produces no send observations or submission; no new public API or lifecycle setting is needed. This completes unreleased 10.0.0 behavior under ADR 0014, ADR 0016, ADR 0029 and ADR 0036.


Original feature request

Why

#688 made Mailer.validate(email) rehearse the real send-time preparation: it applies this Mailer's defaults and overrides, validates the governed Email, renders the MIME message and, by default, processes security and checks the encoded size.

That gives callers a yes/no answer, but it throws the prepared result away. Applications may want to preview, inspect, archive or diagnose what this Mailer would produce without opening an SMTP connection.

Proposed direction

Add a method along these lines:

MailRehearsal rehearse(Email email);
MailRehearsal rehearse(Email email, boolean processSecurityAndValidateSize);

The immutable result should expose useful send-time facts such as:

  • the effective Email after defaults and overrides;
  • the rendered EML bytes, returned defensively;
  • the encoded size;
  • the effective Message-ID;
  • the envelope sender and recipients that would be used.

The supplied Email must remain unchanged and rehearsal must never open an SMTP connection. The existing validate(...) methods can delegate to the same preparation and discard the result.

A rehearsal is a snapshot, not a promise that a later send will produce byte-for-byte identical output. MIME boundaries, generated dates, Message-IDs and cryptographic output may change when the message is built again unless the caller fixes those inputs.

Questions

  • Should the public result expose EML bytes only, or also a mutable Jakarta MimeMessage? EML bytes are safer and provider-neutral.
  • Which fields remain available when security processing and size validation are skipped?
  • Should logging-only and custom-mailer configurations affect rehearsal at all, or should rehearsal always describe Simple Java Mail's normal preparation pipeline?

This builds directly on #688; it does not change the meaning of direct EmailValidator use.

Related history

#451 moved defaults and overrides to Mailer-level send-time governance. #688 then made Mailer.validate(...) run that governed preparation. This issue is the next step: return the prepared result instead of discarding it.

Activity

  1. added this to the 10.0.0 milestone on Aug 25, 2026
  2. moved this from Todo to In Progress in Simple Java Mail 10.0.0on Aug 26, 2026
  3. bbottema commented on Aug 26, 2026

    @bbottema
    OwnerAuthor

    Implemented in 1b116cf3, with website documentation in ec30ef1.

    MailRehearsal rehearsal = mailer.rehearse(email);
    
    Email effectiveEmail = rehearsal.getEffectiveEmail();
    byte[] eml = rehearsal.getEmlBytes();
    long encodedSize = rehearsal.getEncodedSize();
    String messageId = rehearsal.getEmailId();
    List<String> envelopeRecipients = rehearsal.getEnvelopeRecipients();

    Use validate(email) when only a success-or-exception preflight is needed. It delegates to the same no-SMTP preparation and discards the snapshot, so a successful rehearse(email) has already validated the message. rehearse(email, false) returns the base MIME facts while skipping security processing and final-size enforcement.

    The supplied Email remains unchanged. The module tests, Javadocs, website checks, and website build pass.

  4. moved this from Done to In Progress in Simple Java Mail 10.0.0on Nov 2, 2026
  5. bbottema commented on Nov 2, 2026

    @bbottema
    OwnerAuthor
    mailer.close();
    
    MailRehearsal preview = mailer.rehearse(email);
    Files.write(Path.of("preview.eml"), preview.getEmlBytes());

    Rehearsal and validation remain available while the Mailer is closing and after it has closed, using that Mailer's preparation rules. Open calls observe effective Session MIME settings; calls made during or after shutdown use the retained preparation view. Another Mailer reusing the sending Session cannot replace the original owner's policy.

    Use mailer.validate(email) instead when only a success-or-exception check is needed. Full/base modes keep their existing security and size behavior, and exact EML keeps its authoritative bytes.

    Standalone preparation adds no send-drain work and can finish independently of sending-resource cleanup. Work explicitly run inside an accepted send or on the Mailer's owned executor retains that enclosing lifetime and its blocking-close guard. Keep borrowed content available while preparing it.

    Delivered on the unreleased 10.0.0 development branch in 5047ba370 and deb8ce7b8. This follows ADR 0014, ADR 0016, ADR 0029 and ADR 0036.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions