Path: redb-route/redb.Route.Mail/MailConnectionFactory.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Mail/MailConnectionFactory.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Extensions;
using redb.Route.Core;
namespace redb.Route.Mail;
///
/// Named connection factory for mail transports (SMTP, IMAP, POP3). Register it in the route
/// registry and reference it by name from the endpoint URI (connectionFactory=my-mailbox)
/// so mailbox credentials never have to appear in the URI, and therefore can never reach logs,
/// telemetry, or the Tsak dashboard. One factory serves all three protocols.
///
/// Example:
///
/// context.AddToRegistry("corp-mailbox", new MailConnectionFactory
/// {
/// Host = "mail.corp.local",
/// Port = 993,
/// Security = MailSecurityMode.SslOnConnect,
/// Username = "svc-reports",
/// Password = Environment.GetEnvironmentVariable("MAIL_PASSWORD")!
/// });
///
/// // route carries no credentials at all:
/// r.From("imap://?connectionFactory=corp-mailbox&folder=INBOX")
///
///
///
public sealed class MailConnectionFactory
{
// ── Connection ──
/// Mail server hostname or IP.
public string Host { get; set; } = "localhost";
/// Server port (0 = protocol default for the chosen security mode).
public int Port { get; set; }
/// Transport security mode (Auto, None, SslOnConnect, StartTls…).
public MailSecurityMode Security { get; set; } = MailSecurityMode.Auto;
/// Connection timeout in milliseconds.
public int ConnectionTimeout { get; set; } = 30_000;
/// Operation timeout in milliseconds.
public int Timeout { get; set; } = 60_000;
// ── Auth ──
/// Mailbox username.
public string Username { get; set; } = "";
/// Mailbox password.
[Sensitive]
public string Password { get; set; } = "";
/// OAuth2 access token (used instead of the password when set).
[Sensitive]
public string AccessToken { get; set; } = "";
/// Authentication mechanism.
public MailAuthMechanism AuthMechanism { get; set; } = MailAuthMechanism.Auto;
// ── TLS ──
/// Skip server certificate validation (development only!).
public bool TrustAllCertificates { get; set; }
/// Path to the client certificate for mutual TLS.
public string ClientCertPath { get; set; } = "";
/// Password for the client certificate.
[Sensitive]
public string ClientCertPassword { get; set; } = "";
///
/// Copies this factory's connection and credential settings onto the endpoint options, but only
/// for parameters the endpoint URI did not set explicitly — an inline URI value always wins,
/// so existing routes keep their behaviour. is additionally skipped when the
/// URI path already carried the host (imap://mail.corp.local), so a factory can never
/// silently redirect a route to a different server.
///
internal void ApplyTo(MailEndpointOptions options, EndpointUri uri)
{
var supplied = uri.RawParameters;
var hostFromPath = !string.IsNullOrEmpty(uri.Path) && uri.Path != "/";
if (!supplied.ContainsKey(nameof(options.Host)) && !hostFromPath) options.Host = Host;
if (!supplied.ContainsKey(nameof(options.Port))) options.Port = Port;
if (!supplied.ContainsKey(nameof(options.Security))) options.Security = Security;
if (!supplied.ContainsKey(nameof(options.ConnectionTimeout)))
options.ConnectionTimeout = ConnectionTimeout;
if (!supplied.ContainsKey(nameof(options.Timeout))) options.Timeout = Timeout;
if (!supplied.ContainsKey(nameof(options.Username))) options.Username = Username;
if (!supplied.ContainsKey(nameof(options.Password))) options.Password = Password;
if (!supplied.ContainsKey(nameof(options.AccessToken))) options.AccessToken = AccessToken;
if (!supplied.ContainsKey(nameof(options.AuthMechanism))) options.AuthMechanism = AuthMechanism;
if (!supplied.ContainsKey(nameof(options.TrustAllCertificates)))
options.TrustAllCertificates = TrustAllCertificates;
if (!supplied.ContainsKey(nameof(options.ClientCertPath))) options.ClientCertPath = ClientCertPath;
if (!supplied.ContainsKey(nameof(options.ClientCertPassword)))
options.ClientCertPassword = ClientCertPassword;
}
///
/// Shared resolution step for the SMTP / IMAP / POP3 components: looks the named factory up in
/// the registry and applies it. Called after BindFromUri and before Validate().
///
internal static void TryApply(
IRouteContext? context, MailEndpointOptions options, EndpointUri uri,
ILogger? logger, string protocol)
{
if (string.IsNullOrEmpty(options.ConnectionFactory))
return;
// A set-but-unknown name fails loud — never a silent fallback to URI params.
var factory = context.GetRequiredFromRegistry(options.ConnectionFactory);
factory.ApplyTo(options, uri);
}
}