This is an unofficial open-source HTTP client designed for use with the Emailit API.
The client is based on Flurl.
You can simply instantiate EmailitClient using the default constructor:
var client = new EmailitClient();However, in order to use EmailitClient, you will need your Emailit API Key anyway. You can provide it in one of four ways:
- Via the constructor:
var client = new EmailitClient("youApiKey");- Via an
EmailitConfigurationobject:
var config = new EmailitConfiguration
{
ApiKey = "yourApiKey",
};
var client = new EmailitClient(config);- Via a configuration action:
var client = new EmailitClient(config => {
config.ApiKey = "yourApiKey"
})- After initialization using the
UseApiKey()method:
var client = new EmailitClient();
client.UseApiKey("yourApiKey");Before sending an email using EmailitClient, you need to create an email message. Emailit messages are represented by the EmailitMessage model. You can create one in two ways:
- Using an
EmailitMessageliteral:
var message = new EmailitMessage
{
From = some@email.com",
To = "[email protected]",
Subject = "Some subject",
Text = "Some text",
}- Using an
EmailitMessageBuilder
var message = new EmailitMessageBuilder()
.From("[email protected]")
.To("[email protected]")
.ReplyTo("[email protected]")
.Subject("Some subject")
.WithHtmlContent("<h1>Some HTML</h1>")
.Message;NOTE: It is recommended to build EmailitMessage instances using EmailitMessageBuilder, as it provides some basic null checks and validation. If you use EmailitMessage directly, you must handle validation yourself..
EmailitMessage provides support for email attachments, represented by EmailitAttachment object:
- You can initialize it via the constructor:
var attachment = new EmailitAttachment(
"someattachment.pdf",
"<based-64 array of bytes>"
);- Or via object literal:
var attachment = new EmailitAttachment
{
FileName: "somefile.pdf",
Content: "<based-64 array of bytes>"
};- You can also create it directly from a byte array:
var attachment = EmailitAttachment.FromByteArray(<array of bytes here>, "filename.png");Once created, attachments can be easily added to an EmailitMessage:
var attachment = new EmailitAttachment();
var message = new EmailitMessage();
message.AddAttachment(attachment);Attachment can be also added as array of bytes, directly to EmailitMessage:
message.AddAttachment(<array of bytes>, "filename.png");Once your EmailitMessage is ready, you can send it using:
EmailitClient:
var client = new EmailitClient("yourApiKey");
var message = new EmailitMessageBuilder()
.From("[email protected]")
.To("[email protected]")
.ReplyTo("[email protected]")
.Subject("Some subject")
.WithTextContent("Some text")
.Message;
await client.SendEmailAsync(message);- Directly from
EmailitMessage(using an extension method):
var message = new EmailitMessageBuilder()
.From("[email protected]")
.To("[email protected]")
.ReplyTo("[email protected]")
.Subject("Some subject")
.WithTextContent("Some text")
.Message;
await message.SendAsync("yourApiKey");The default EmailitClient settings are defined in the protected virtual DefaultBaseUrl property, which:
- Provides default
JsonSerializerOptionsfor theSystem.Text.Jsonserializer - Verifies that the
ApiKeyis supplied (throws if not) - Supplies the API Call with the provided
ApiKey - Builds a basic
Urlobject (a Flurl object) using default_baseUrland_version
To override the default behavior, simply create a custom class that derives from 'EmailitClient'. You can then redefine the 'DefaultBaseUrl' behavior as needed.
EmailitClient covers three crutial Emailit functionalities:
- Managing Credentials
- Managing Sending Domains
- Sending Emails
It does not cover managing audiences, contacts, events and campaigns. Feel free to contribute and help expand this functionality!
In the nearest future, I'd like to:
- Create a separate package that provides
FluentValidationfor Emailit objects - Implement missing
EmailitClientmethods and models (see above) - Enhance support for Dependency Injection (currently uses a simple “clientless” pattern from Flurl)
- Improve error handling, especially for HTTP status codes
Feel free to contribute!