# SMTP

## Description

SMTP is the simplest way to send transactional email through any standards-compliant mail server (Office365, Gmail with app password, on-prem SMTP, etc.).

Your application code depends on `IFlexEmailProvider`; the SMTP provider supplies that implementation.

## Configuration in DI

Register only the provider.

```csharp
services.AddFlexSmtpEmailProvider(configuration);
```

## appsettings.json

Configuration is read from `FlexBase:Providers:Email:Smtp`.

```json
{
  "FlexBase": {
    "Providers": {
      "Email": {
        "Smtp": {
          "Host": "smtp.office365.com",
          "Port": 587,
          "UseSsl": true,
          "UseStartTls": true,
          "Username": "user@company.com",
          "Password": "<store-in-secrets>",
          "DefaultFromEmail": "noreply@company.com",
          "DefaultFromName": "Company",
          "MaxRetries": 3,
          "Timeout": "00:00:30"
        }
      }
    }
  }
}
```

## Examples (template-based)

This mirrors the generated PostBus handler shape (you do not register the handler manually).

```csharp
using Microsoft.Extensions.Logging;
using Sumeru.Flex;
using System.Threading.Tasks;

namespace {YourApplication}.PostBusHandlers.Email;

public partial class SendSmtpEmailHandler : ISendSmtpEmailHandler
{
	protected string EventCondition = string.Empty;

	protected readonly ILogger<SendSmtpEmailHandler> _logger;
	protected readonly IFlexHost _flexHost;
	protected readonly IFlexEmailProvider _emailProvider;

	protected FlexAppContextBridge? _flexAppContext;

	public SendSmtpEmailHandler(
		ILogger<SendSmtpEmailHandler> logger,
		IFlexHost flexHost,
		IFlexEmailProvider emailProvider)
	{
		_logger = logger;
		_flexHost = flexHost;
		_emailProvider = emailProvider;
	}

	public virtual async Task Execute(SendSmtpEmailCommand cmd, IFlexServiceBusContext serviceBusContext)
	{
		_flexAppContext = cmd.Dto.GetAppContext();  //do not remove this line

		await _emailProvider.SendHtmlAsync(
			to: cmd.Dto.To,
			subject: cmd.Dto.Subject,
			htmlBody: cmd.Dto.Html,
			from: cmd.Dto.From);

		await this.Fire(EventCondition, serviceBusContext);
	}
}
```

## Provider considerations

* Prefer StartTLS on port 587 for most providers.
* Use secrets storage for `Password`.
