E Mail

Description

Email providers in FlexBase expose a unified interface (IFlexEmailProvider) for sending transactional email via SMTP, SendGrid, AWS SES, Azure Communication Services, and more.

Your application code should depend on IFlexEmailProvider.

Important concepts

  • IFlexEmailProvider is the contract: send via SendAsync(...), SendHtmlAsync(...), SendToManyAsync(...), SendBulkAsync(...), and SendTemplatedAsync(...).

  • Provider bridge: generated infrastructure commonly registers IFlexEmailProviderBridge (which also implements IFlexEmailProvider) so behavior can be overridden safely.

  • Templates are optional: template rendering is supported via IFlexEmailTemplateEngine (and provider-specific template features vary).

Configuration in DI

Only register the provider—Flex auto-wires generated PostBus handlers/plugins that consume the interface.

public static class OtherApplicationServicesConfig
{
    public static IServiceCollection AddOtherApplicationServices(
        this IServiceCollection services,
        IConfiguration configuration)
    {
        // Pick ONE (or register multiple with different compositions).
        services.AddFlexSmtpEmailProvider(configuration);
        // services.AddFlexSendGridEmailProvider(configuration);
        // services.AddFlexAwsSesEmailProvider(configuration);
        // services.AddFlexAzureCommunicationEmailProvider(configuration);
        return services;
    }
}

appsettings.json

Email provider configuration is read from FlexBase:Providers:Email:<Provider>.

Examples (template-based)

This mirrors the generated PostBus handler template shape. You do not register the handler manually.

Provider considerations

  • Keep secrets (SMTP password, SendGrid API key, AWS keys, ACS connection string) out of source control.

  • Use TestConnectionAsync(...) during startup checks/health probes.

    public async Task SendWelcomeEmailAsync(string email, string name) { var result = await _emailProvider.SendAsync( to: email, subject: "Welcome!", body: $"Hello {name}, welcome to our platform!" );

    } }

Full Message Object

Templated Emails

Bulk Emails

Attachments

Key Points to Consider

Best Practices

  1. Use Templates - Define templates for common emails (welcome, reset, etc.)

  2. Validate Emails - Use ValidateEmailFormat() before sending

  3. Handle Errors - Always check IsSuccess and log failures

  4. Use Console in Dev - Avoid sending real emails during development

  5. Include Text Fallback - Set both HtmlBody and TextBody

  6. Test Connection - Use TestConnectionAsync() on startup

  7. Monitor Delivery - Track MessageId for delivery status

Email Validation

Error Handling

Testing Connection

Multiple Providers

Examples

Complete Email Service

Testing

See Also

Last updated