Skip to content

Explain SMTP connection capabilities and TLS negotiation #733

Closed

Description

@bbottema

Why

A connection test can succeed while two SMTP endpoints offer very different features or security. Developers need to see what the server advertised, what changed after STARTTLS, and which part of connection setup failed without reading a protocol debug log or sending an email.

This is step 4 of Phase 3 in #722. Keep testConnection() as the simple health check; add a separate, immutable, safe diagnostic report. Implementation is targeted at 10.0.0 on codex/10.0.0.

Scope

  • Report the endpoint, greeting, separate pre-TLS/post-TLS EHLO snapshots, STARTTLS outcome, available TLS/peer-identity facts, authentication capabilities and optional authentication outcome, SIZE and other advertised SMTP extensions.
  • Preserve unknown/unsupported facts explicitly. Advertised capability is not proof that a message used it, and successful connection setup is not proof of mail delivery.
  • Retain a useful partial report when setup fails; escape server-controlled text and keep credentials, tokens, authentication exchanges and message content out of the safe report.
  • Use a dedicated connection so diagnosis does not consume or alter a pooled send's connection. Preserve caller-owned Session/provider/socket settings and make provider limitations explicit.
  • Reuse supported Jakarta Mail and Angus hooks. No reflection, debug-log scraping or dependency on an upstream Angus PR.
  • Follow the root API expansion and coding guides. Include classpath/JPMS and applicable CLI/configuration checks, local scripted SMTP/TLS tests, a manual demo and website diagnostics examples.
  • Review the shared architecture image/catalogue; update them only if the responsibilities change.

Implementation

Completed and accepted on 15 September 2026. The Java and CLI implementations and documentation are committed and pushed on the 10.0.0 development branches; this is unreleased 10.0.0 work.

SmtpConnectionReport report = mailer.sync().probeConnection(); // no authentication
logger.info("SMTP connection:\n{}", report);

report.getEffectiveCapabilities().ifPresent(capabilities ->
    logger.info("DSN advertised: {}", capabilities.supports("DSN")));

// Explicitly test credentials, or run the probe in the background:
SmtpConnectionReport authenticated = mailer.sync().probeConnection(true);
CompletableFuture<SmtpConnectionReport> pending = mailer.async().probeConnection();

Implemented:

  • Separate before-/after-TLS snapshots, optional positive SIZE limits, TLS metadata where safely obtainable, actual authentication facts and sanitized partial failure reports.
  • A dedicated connection outside the send pool. Default no-auth probes do not consult cached credentials, authenticators or token providers. Explicit authentication retains the ordinary password/OAuth2 path.
  • Caller Session/provider settings, socket factories, hostname-verifier decisions and proxy routing are preserved. A configured verifier class can leave TLS metadata unavailable; no trust decision is weakened to fill in a report.
  • Rejected post-TLS EHLO cannot reuse plaintext advertisements or proceed into AUTH with stale facts. Missing/oversized discovery remains unknown rather than a fabricated capability list.
  • Local SMTP/TLS integration coverage, public classpath/JPMS checks, a manually runnable loopback demo, README/release entries and website Diagnostics examples.
  • A separately packaged third-party adapter/provider fixture works through classpath and JPMS discovery with Angus absent. Missing capability or TLS metadata stays unavailable without turning a successful connection into a failure; no compatibility claim is made for untested real providers.
  • The shared send infographic was reviewed unchanged: this diagnostic path does not introduce Email, pooled-send, observer or send-control state.

The CLI exposes the same report through sjm probe --mailer:withSMTPServerHost smtp.example.org. Add --authenticate to test SMTP credentials. One-shot and daemon commands print the report and return 0 for success, 3 for a failed/unsupported probe, or 2 for invalid arguments. Authentication is an invocation choice, not a cached Mailer setting. A configured nonzero pool core size can separately warm and authenticate send connections when the Mailer is built; use core size 0 (including cluster overrides) to avoid that warm-up.

The probe explicitly connects even in logging-only mode. Unsupported providers and CustomMailer return an unsupported report without connecting. Provider connection/read timeouts apply, not total mail-send deadlines; cancelling the returned future does not abort socket I/O. Existing testConnection() and ordinary send behavior are unchanged.

No new persistent configuration setting or Email-governance field is needed: the Mailer already supplies endpoint/TLS/proxy/timeouts, Spring's injected Mailer exposes the same API, and authentication is an invocation choice.

  • Characterize supported Angus greeting/EHLO/STARTTLS hooks with executable local tests.
  • Implement and test the Java report and sync/async probe against real local peers.
  • Verify dedicated-connection ownership, caller Session/credential/security preservation, proxy routing and pool independence.
  • Verify explicit unsupported behavior for other providers and CustomMailer.
  • Add the local demo, safe illustrative output, website diagnostics guidance and release entry.
  • Add a runtime fixture for a third-party adapter returning partial detail.
  • Add CLI probe-command integration and verify one-shot/daemon output and exit semantics.
  • Complete production-diff and holistic review before committing or closing.

The following step's authentication-without-verified-TLS policy is separate. PIPELINING/CHUNKING implementation remains parked under #699; reporting an advertisement does not implement that feature.

Provider references: Angus connection/EHLO behavior, Angus TLS hooks.

Implementation plan

The execution-view migration is complete in #734, also under #722. The examples above use the accepted view-based API. The probe entry points, implementation, CLI command and third-party adapter fixture are complete in this issue. The separate Phase 3 authentication-policy step remains open.

Activity

  1. added this to the 10.0.0 milestone on Sep 11, 2026
  2. moved this from Todo to In Progress in Simple Java Mail 10.0.0on Sep 11, 2026
  3. moved this from In Progress to Done in Simple Java Mail 10.0.0on Sep 15, 2026
  4. bbottema commented on Sep 15, 2026

    @bbottema
    OwnerAuthor

    Implemented for the upcoming 10.0.0 release:

    SmtpConnectionReport report = mailer.sync().probeConnection();
    System.out.println(report);

    Example output, abbreviated:

    SMTP connection probe: SUCCESS
    STARTTLS: completed
    After TLS: {AUTH=[LOGIN PLAIN], DSN=[], SIZE=[26214400]}
    Authentication: not requested
    ...
    

    Use probeConnection(true) to test configured SMTP credentials, or sjm probe --authenticate from the CLI. Probing opens and closes a dedicated connection without sending an email; unavailable provider details stay explicit.

  5. bbottema commented on Nov 1, 2026

    @bbottema
    OwnerAuthor

    Development addenda for unreleased 10.0.0

    These follow-ups complete this feature's 10.0.0 implementation. Dates identify the original development records; before examples describe earlier development revisions.

    2026-11-01: Report the actual configured SMTP probe port for hexadecimal values

    A supplied Jakarta Mail Session can use a valid hexadecimal SMTP port, but the connection probe report parses only decimal values. Angus connects to the requested port while the report describes the protocol default.

    Properties properties = new Properties();
    properties.setProperty("mail.smtp.host", "localhost");
    properties.setProperty("mail.smtp.port", "0x9db"); // 2523
    Mailer mailer = mail.mailerBuilder(Session.getInstance(properties)).buildMailer();
    SmtpConnectionReport report = mailer.sync().probeConnection();

    The report should identify port 2523. Align supported provider endpoint interpretation with its actual property lookup and integer grammar, including inherited String values and direct Integer values. Preserve protocol/implicit-TLS defaults and the existing meaning of absent, invalid and sentinel values; do not mutate the supplied Session or invent facts for unknown providers.

    This correction targets the 10.0.0 development branch and follows ADR 0020 and ADR 0039.

    Completed behavior — 2026-11-01

    Properties properties = new Properties();
    properties.setProperty("mail.smtp.host", "localhost");
    properties.setProperty("mail.smtp.port", "0x9db"); // 2523
    
    try (Mailer mailer = mail.mailerBuilder(Session.getInstance(properties)).buildMailer()) {
        SmtpConnectionReport report = mailer.sync().probeConnection();
        System.out.println(report.getPort()); // 2523
    }

    The 10.0.0 development branch now reports configured ports using the supported provider's interpretation. Direct Integer values, decimal Strings and lowercase-0x hexadecimal Strings keep their meaning. Inherited String defaults apply only when there is no direct entry; an opaque direct value keeps the provider default. The probe leaves the supplied Session's properties intact.

    Implemented in 36c33f6e, following ADR 0020 and ADR 0039. The accompanying guidance is in the main documentation commit and the website documentation commit. This is development-branch delivery, not a published release.

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