Skip to content

Auth email delivery ​

The built-in Auth email sender delivers messages through SMTP with MailKit. It reads the nested GrydAuth:Email configuration when a message is sent. SMTP settings are not required to start an application that does not send email, and a host that registers its own IEmailService does not need the built-in SMTP settings.

json
{
  "GrydAuth": {
    "PasswordReset": {
      "DefaultResetPageUrl": "https://app.example.com/reset-password",
      "AllowedResetPageOrigins": ["https://app.example.com"]
    },
    "Email": {
      "Enabled": true,
      "FromEmail": "noreply@example.com",
      "FromName": "Example App",
      "Smtp": {
        "Host": "smtp.example.com",
        "Port": 587,
        "EnableSsl": true
      }
    }
  }
}

Store SMTP credentials in the deployment's secret store or environment, rather than committing them. ASP.NET configuration environment variables can provide them as GrydAuth__Email__Smtp__Username and GrydAuth__Email__Smtp__Password. EnableSsl: true requires STARTTLS; the sender will fail if the server does not offer it. Set EnableSsl: false only when intentionally using a trusted plaintext development server, such as a loopback SMTP receiver. The sender requires a valid sender address, SMTP host and port, and either both SMTP credentials or neither.

The default sender does not implement SendGrid. Any configured SendGrid block is rejected, even when SMTP is also present, so configuration cannot silently select the wrong provider. To use SendGrid or another provider, register a custom IEmailService before AddGrydAuth; the Auth infrastructure uses TryAddScoped and preserves that registration. A custom implementation can also provide a development recorder without contacting an SMTP server.

For another provider, register its implementation before the Auth facade. Its reset method receives the complete, already validated reset URL:

csharp
using GrydAuth.Application.Common.Interfaces;
using GrydAuth.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

builder.Services.AddScoped<IEmailService, MyProviderEmailService>();
builder.Services.AddGrydAuth(builder.Configuration);

For local development, this opt-in recorder does not deliver mail. It counts three fixed message categories without retaining or logging recipients, subjects, bodies, or reset links:

csharp
using GrydAuth.Application.Common.Interfaces;
using GrydAuth.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using System.Threading;

if (builder.Environment.IsDevelopment())
{
    builder.Services.AddSingleton<DevelopmentEmailRecorder>();
    builder.Services.AddSingleton<IEmailService>(services =>
        services.GetRequiredService<DevelopmentEmailRecorder>());
}

builder.Services.AddGrydAuth(builder.Configuration);

public sealed class DevelopmentEmailRecorder : IEmailService
{
    private int _messages;
    private int _welcomeMessages;
    private int _passwordResetMessages;

    public int Messages => Volatile.Read(ref _messages);
    public int WelcomeMessages => Volatile.Read(ref _welcomeMessages);
    public int PasswordResetMessages => Volatile.Read(ref _passwordResetMessages);

    public Task SendEmailAsync(string to, string subject, string body, CancellationToken cancellationToken = default) =>
        Record("message", cancellationToken);

    public Task SendWelcomeEmailAsync(string to, string firstName, CancellationToken cancellationToken = default) =>
        Record("welcome", cancellationToken);

    public Task SendPasswordResetEmailAsync(
        string to,
        string resetLink,
        DateTime expiresAt,
        CancellationToken cancellationToken = default) =>
        Record("password-reset", cancellationToken);

    private Task Record(string category, CancellationToken cancellationToken)
    {
        cancellationToken.ThrowIfCancellationRequested();
        switch (category)
        {
            case "message": Interlocked.Increment(ref _messages); break;
            case "welcome": Interlocked.Increment(ref _welcomeMessages); break;
            case "password-reset": Interlocked.Increment(ref _passwordResetMessages); break;
        }
        return Task.CompletedTask;
    }
}

POST /api/v1/auth/request-password-reset returns the same public success for an admitted valid request when the account is missing or ineligible, delivery fails, or the account-specific active token limit is reached. Existing request validation and request/IP rate limits still apply. The public success does not confirm that an email was sent. For the built-in sender, successful SMTP submission means the provider accepted the message; it does not confirm mailbox delivery. SMTP failures are surfaced to operational logs without logging recipient, subject, message body, reset link, credentials, or raw server response. Caller cancellation continues to propagate. If delivery fails after a reset token is persisted, that token remains until it expires or is invalidated by a later reset request or password change. Admin-triggered reset delivery failures from the built-in sender return HTTP 503 with code EMAIL_DELIVERY_UNAVAILABLE and a generic, localized service-unavailable detail; they prevent a successful-reset audit event. Exceptions from a host's custom IEmailService continue through that host's configured exception mapping and may use a different status. Anonymous reset requests keep their uniform public success response on delivery failure.

The reset flow builds and validates the reset URL before calling IEmailService; implementations receive that complete link and must not rebuild it from configuration. The built-in sender HTML encodes the supplied link and welcome name before placing them in HTML messages.

Use the nested Smtp keys shown above. Legacy flat keys such as Provider, SmtpHost, SmtpPort, SmtpUsername, SmtpPassword, and UseSsl are not consumed by this sender. Configure the reset page with DefaultResetPageUrl; legacy BaseUrl and ResetPath entries do not configure it.

Released under the MIT License.