Overview - what this Library is?
We want to provide our clients a way to use our system however they like. That is why we have introduced the Integration Libraries - a group of functions and declarations to help you implement our API to your system.
How to download and implement the Library
All of our libraries are available here, whereas the specific library for C# can be found at the following link. Alternatively, you can download the .NET Standard version from here.
After you have downloaded the library, the easiest way to implement it would be to use the Visual Studio's Build Action option in the file's Properties.
Right-click on any place in your solution in which you would like to include our library, select Add -> Existing Item (or use the shortcut Shift + Alt + A)and choose the file ElasticEmailClient.cs from the File Explorer.
Then, open the Properties window by right-clicking on our library file in the project and select the Properties option (or left clicking on the file and using the shortcut Alt + Enter).
From there, select the Compile option for the Build Action property.
Now you add the namespace under which our library can be found, ElasticEmailClient, in every file in which we would want to have access to the library.
How to start working with the Library on your project
All of our library methods are available using the static class named API. Whether you want to send an email or modify your template, you can do so using the API class.
For creating a subclass for one of the calls, or (most of the time) to store a result of the call, you can use the ApiTypes class.
Before everything else, we need to set up an ApiKey, which is a unique identifier of your account. You can do this in two ways:
First way: Open up ElasticEmailClient.cs by double-clicking in the Solution Explorer. You should have all of the regions collapsed except for our APIstatic class. Here you can find the fields ApiKey and ApiUri. Change the former's value to your ApiKey available to you on our dashboard. Don't mind the ApiUri field, it is best to leave it untouched.
Second way: Simply overwrite the ApiKey field's value of the static class Api at the start of your code:
Api.ApiKey = "11111111-2222-3333-4444-555555555555";
This can also be used to swap between the accounts while your program is running, so you can keep this line in mind.
Some basic API calls made using this Library
Sending a merged email to multiple recipients, provided as an attachment:
static void Main(string[] args)
{
string subject = "Your subject";
string fromEmail = "youremail@test.com";
string fromName = "Your Company Name";
string bodyText = "Text body";
string bodyHtml = "<h1>Hello, {username}.</h1>";
var attachments = new List<ApiTypes.FileData>() { ApiTypes.FileData.CreateFromFile("C:/Users/recipients.csv") };
string filenameWithRecipients = "recipients.csv"; // same as the file above
ApiTypes.EmailSend result = null;
try
{
result = Api.Email.Send(subject: subject, from: fromEmail, fromName: fromName, bodyText: bodyText, bodyHtml: bodyHtml, attachmentFiles: attachments, mergeSourceFilename: filenameWithRecipients);
}
catch (Exception ex)
{
if (ex is ApplicationException)
Console.WriteLine("Server didn't accept the request: " + ex.Message);
else
Console.WriteLine("Something unexpected happened: " + ex.Message);
return;
}
Console.WriteLine("MsgID to store locally: " + result.MessageID); // Available only if sent to a single recipient
Console.WriteLine("TransactionID to store locally: " + result.TransactionID);
}
An example of loading info about your own account and displaying the price per email:
static void Main(string[] args)
{
ApiTypes.Account account = null;
try
{
account = Api.Account.Load();
}
catch (Exception ex)
{
if (ex is ApplicationException)
Console.WriteLine("Server didn't accept the request: " + ex.Message);
else
Console.WriteLine("Something unexpected happened: " + ex.Message);
return;
}
Console.WriteLine(account.PricePerEmail); // What a deal!
}
One of many ways to save your contacts:
static void Main(string[] args)
{
string[] contacts = { "test1@test.com", "test2@test.com" };
try
{
Api.Contact.QuickAdd(contacts, notes: "Testing QuickAdd");
}
catch (Exception ex)
{
if (ex is ApplicationException)
Console.WriteLine("Server didn't accept the request: " + ex.Message);
else
Console.WriteLine("Something unexpected happened: " + ex.Message);
return;
}
}
License
All code samples are licensed under MIT license.