> For the complete documentation index, see [llms.txt](https://docs.flexbase.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flexbase.in/data-and-providers/providers/e-mail/send-grid.md).

# Send Grid

## Description

SendGrid is a transactional email provider suited for production usage (delivery, tracking, analytics). Your application code depends on `IFlexEmailProvider`.

## Configuration in DI

Register only the provider.

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

## appsettings.json

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

```json
{
  "FlexBase": {
    "Providers": {
      "Email": {
        "SendGrid": {
          "ApiKey": "<store-in-secrets>",
          "DefaultFromEmail": "noreply@company.com",
          "DefaultFromName": "Company",
          "EnableTracking": true,
          "EnableClickTracking": true,
          "EnableOpenTracking": true,
          "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 SendSendGridEmailHandler : ISendSendGridEmailHandler
{
	protected string EventCondition = string.Empty;

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

	protected FlexAppContextBridge? _flexAppContext;

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

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

		var result = await _emailProvider.SendAsync(
			to: cmd.Dto.To,
			subject: cmd.Dto.Subject,
			body: cmd.Dto.Body,
			from: cmd.Dto.From);

		cmd.Dto.MessageId = result.MessageId;
		cmd.Dto.IsSuccess = result.IsSuccess;

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

## Provider considerations

* `ApiKey` must be stored in secrets.
* Tracking flags affect analytics and privacy expectations.
