Email Without Secrets: Making ICG's .NET Email Libraries AI-Agent Friendly

More and more of our development work happens next to an AI coding agent. That might be GitHub Copilot in Visual Studio, Claude Code in a terminal, or an agent running in a cloud sandbox. These tools are good at writing a feature, running it, and checking the result. Email has always been an awkward exception.

To send email, the agent needs a SendGrid or Resend API key or SMTP credentials. That leaves two bad options. You can hand a real production-grade secret to a tool (and to every log, transcript, and sandbox it touches). Or you can accept that the agent writes email code it can never verify. Neither is great, and honestly, human developers have the same problem every time they clone a repo and the first run fails because a key is missing.  Or worse, they accidentally email a bunch of people something they shouldn't have gotten!

With the latest releases of our open-source ICG.NetCore.Utilities.Email libraries, there's a third option: don't send the email at all. Capture it, and make it easy to inspect.

The Short Version

  • No secrets in development. Outside Production, if the provider's secret isn't configured, email is captured to a local .email-outbox/ folder instead of failing.
  • Production is protected. Capture never switches on automatically in Production. If a non-live mode is configured there by mistake, the application fails at startup instead of silently dropping customer email.
  • Same code, any provider. The delivery settings live in the core configuration, so the behavior is identical whether you use SendGrid, Resend, or SMTP. Switching providers stays a configuration change.
  • Ways to see what was "sent": plain files, a development-only web viewer, test assertions, and an MCP server so AI agents can read captured email directly.

Delivery Modes

Everything is driven by a new EmailDeliveryOptions section that sits next to your existing email configuration:

{
  "EmailDeliveryOptions": {
    "Mode": "Capture"
  }
}

There are four modes:

Mode What happens
Live Sends through the provider, exactly as before (the default)
Capture Writes the message to the outbox; nothing is sent
CaptureAndLive Sends it and keeps a copy in the outbox
Redirect Sends it, but to a single address you choose (useful for QA environments)

In most cases you won't configure anything. With AutoCaptureWhenUnconfigured (on by default), a non-Production environment with no API key or SMTP server just captures email and logs a warning. New team members, CI runs, and AI agents can clone the repository and run it right away. We even auto-add a git ignore file to the output folder to make it easy!

"But What If This Gets Turned On in Production?"

This was the first question I asked when we designed this, and it shaped everything. A setting that silently stops sending password resets is a serious risk, so the rules are strict:

  1. Automatic capture never applies in Production. If the app can't determine its environment, it's treated as Production.
  2. Any non-Live mode in Production fails startup validation. You find out at deploy time, not from a customer.
  3. If you really want capture in Production (for example, a locked-down demo), you have to opt in explicitly with AllowNonLiveModeInProduction. It's deliberately named so nobody sets it by accident.

Seeing What Was Sent

Capturing email only helps if you can look at it. There are four ways, depending on who's looking.

1. The Outbox Folder

Each captured message is written to .email-outbox/ as JSON metadata, the rendered HTML, and the raw payload (an .eml file for SMTP, the provider's JSON payload for SendGrid and Resend). The folder adds its own .gitignore so captured messages don't get committed, and old messages are pruned automatically.

2. A Development-Only Viewer

Add ICG.NetCore.Utilities.Email.AspNetCore and one line:

app.MapIcgEmailOutbox(); // browse to /_dev/emails

You get a simple inbox of everything the app has "sent", with a sandboxed preview of each message and a JSON API. It only maps in Development unless you explicitly opt in.

3. Assertions in Your Tests

The new ICG.NetCore.Utilities.Email.Testing package swaps in an in-memory outbox and gives you readable assertions:

services.UseIcgEmailCapture();

// ... exercise the feature ...

await outbox.ShouldHaveSentAsync(new EmailCriteria
{
    Recipient = "customer@example.com",
    SubjectContains = "Your order"
});

Integration tests can now verify real email behavior with no mocks of provider SDKs and no test API keys.

4. An MCP Server for AI Agents

This is the part I'm most excited about. ICG.NetCore.Utilities.Email.OutboxMcp is a Model Context Protocol server, shipped as a .NET tool, that gives AI agents direct access to the outbox. With .NET 10, you can add it to a repository with no install step:

{
  "servers": {
    "icg-email-outbox": {
      "type": "stdio",
      "command": "dnx",
      "args": ["ICG.NetCore.Utilities.Email.OutboxMcp", "--yes"]
    }
  }
}

The agent gets tools to list, search and read captured messages (including the rendered HTML and raw payload), and to clear the outbox. A typical loop looks like this:

  1. clear_outbox
  2. Trigger the feature (run a test, call an endpoint)
  3. list_emails, then get_email latest
  4. Check the recipients, subject and content, then fix and repeat

The agent can now finish a task like "add a welcome email when a user registers" end to end and prove the email is right, without ever seeing a credential. The package also includes the standard .mcp/server.json manifest, so its NuGet.org page has ready-to-copy configuration for VS Code and Visual Studio.

Getting Started

Update to the latest packages:

  • ICG.NetCore.Utilities.Email.SendGrid, ICG.NetCore.Utilities.Email.Resend or ICG.NetCore.Utilities.Email.Smtp
  • Optionally add ICG.NetCore.Utilities.Email.Testing to your test projects and ICG.NetCore.Utilities.Email.AspNetCore to your web app

Then remove the API key from appsettings.Development.json (it shouldn't be there anyway) and run your app. Existing Production configurations keep working unchanged, because Live is still the default.

Each repository also now includes an AGENTS.md file, so AI tools working in your codebase or ours know how to use capture mode instead of asking for secrets.

Why This Matters

Secrets are one of the biggest sources of friction, and risk, when bringing AI tools into real development work. Our approach is to design libraries so the safe path is the default: agents and developers get a fast, fully verifiable loop, and Production keeps its guardrails. Email was an obvious place to start, and these patterns apply to many other integrations.

The libraries are open source under the MIT license. You can find the code, documentation, and issue trackers on GitHub:

I'd love to hear how you're handling secrets and AI agents in your own projects. Leave a comment or reach out, and if you try this out, let us know how it goes.

Categories

Share