A Mailgun email API client
$ dotnet add package AO.MailgunThis came from a need to send emails with different providers -- Mailgun and Smtp2Go -- leveraging some shared functionality for logging and environment-specific behavior (different behavior for QA vs prod, for example).
I started with a base abstract class MailClientBase, then added implementations for
I offer these as NuGet packages:
All mail clients inherit from MailClientBase<TOptions>. Use TOptions to define the settings required for that client. Examples: Mailgun Options, Smtp2Go Options.
For the two email providers in this repo I'm implementing, I overrode the abstract method SendImplementationAsync.
If you want to take advantage of some optional functionality, you'll need to create your own subclass of MailgunClient or Smtp2GoClient. These are the optional overrides:
A common requirement with email is you want to make sure QA and local dev environments don't send to production recipients. One reason for this library is that I wanted a definitive solution for this that works with any email provider. I approached this by having a common configuration property OptionsBase.SendMode. All mail client implementations must base their TOptions class on OptionsBase, so they will inherit the SendMode property.
SendMode has 3 options:
LogOnly disables all sending, suitable for local dev testing. In this scenario, you'd override LogMessageAsync to capture what would be sent in production. This can still be useful in production (not merely for development) if you want to capture outgoing emails in a database table, for example.Filter allows conditional sending. You would override MailClientBase.FilterMessageAsync to control whether a message sends. You could use this to check for a certain domain, block or allow specific recipients, and so on. Suitable for QA environments or local dev testing. By default, all messages are rejected, so you'll need to override this if you use the Filter send mode.SendAll sends everything, intended as the production settingWhen you send a message, the SendMode is checked within method ShouldSendAsync.
Html email rendering is a related but separate concern from this. I have a different project RazorToString that addresses this specifically.