Skip to content

Use the selected protocol's username for OAuth connections #38

Closed

Description

@bbottema

When an OAuth connection uses SMTPS, the pool can explicitly authenticate with an unrelated SMTP username left on the same Session. This affects released 4.4.0 and makes credential selection depend on whether the connection is pooled.

Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtps");
properties.setProperty("mail.smtps.host", "smtp.example.org");
properties.setProperty("mail.smtps.user", "current@example.org");
properties.setProperty("mail.smtps.auth.mechanisms", "XOAUTH2");
properties.setProperty("mail.smtp.user", "old@example.org");
properties.setProperty(
    SmtpConnectionPool.OAUTH2_TOKEN_PROPERTY, "current-account-token");
Session session = Session.getInstance(properties);

try (SmtpTransportLease lease = pool.claimTransport(session)) {
    // The connection must authenticate as current@example.org.
}

The current OAuth allocator always supplies mail.smtp.user explicitly. The selected SMTPS provider consequently receives old@example.org together with the current account's token.

Let the selected connection's protocol determine username resolution, preserving Jakarta Mail's ordinary username fallback and supplied Session properties. SMTP and STARTTLS use the SMTP settings; SMTPS uses the SMTPS settings. Apply the same rule to initial allocation and reconnection for both fixed tokens and token suppliers. A connected lease still reuses its connection without another supplier call. Preserve authentication failures, cancellation and cleanup.

The fixed-token path originated with Simple Java Mail #421; #9 added connection-time suppliers. This correction needs no new public API or configuration option and belongs in compatible patch release 4.4.1. Simple Java Mail's matching protocol applicability decisions are ADR 0015, ADR 0038 and ADR 0039.

Activity

  1. added this to the 4.4.1 milestone on Nov 5, 2026
  2. self-assigned this
    on Nov 5, 2026
  3. bbottema commented on Nov 5, 2026

    @bbottema
    MemberAuthor
    properties.setProperty("mail.transport.protocol", "smtps");
    properties.setProperty("mail.smtps.user", "current@example.org");
    properties.setProperty("mail.smtp.user", "other@example.org");
    
    try (SmtpTransportLease lease = pool.claimTransport(session)) {
        // The selected SMTPS provider resolves the account normally.
    }

    Available in 4.4.1. OAuth connections now preserve the selected provider's username resolution instead of explicitly supplying the SMTP username for every protocol. Fixed tokens and suppliers retain normal fallback, reconnection and Session ownership behavior.

    The correction is in 132a5f5. SJM #966 carries the matching dependency update for the 10.0.0 development branch.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions