Elastic Email C# API Library

Introduction

This page will help you easily integrate with Elastic Email using the C# library. You can find our whole downloadable Javascript repository on GitHub here. If you need API documentation, you can find it here.

When you integrate Elastic Email with your application, you can interact with your Elastic Email account directly by our API. You can easily integrate your email flow, manage your contacts and templates, send emails, and track email statistics directly from your app.

Elastic Email API v4 uses REST architecture, which allows you to perform various code actions, including directly through your app.

The maximum email size of your entire message or message + attachment cannot exceed 20MB.

The attachment format is the file’s content as a byte array or a Base64 string.

The maximum number of recipients has no limit for one campaign. It depends on the pricing plan.

The API has a limit of 20 concurrent connections and a hard timeout of 600 seconds per request.

On this page, you will find how to authenticate your application and what the requirements for the integration are. You will be also provided with a quick start guide on how to start using API, followed by code samples. 

Authentication

To provide valid authentication and start using our API, you will need an API key. To generate your API key, enter settings on your Elastic Email account and go to Settings -> Manage API Keys -> Create or you can also click on the link: 

https://elasticemail.com/account#/settings/new/create-api

Ceater API key

At this point, you can set custom permissions and optional access for your API key. 

Security tip: The restriction forces the API Key to work only with an IP or IP range that will be specified in this field.

Create API key 2

Once you create your API key, keep it safe as it will be used for every API call you make in order to identify you and confirm your account’s credentials. You can create either up to 15 or an unlimited amount of API keys based on your pricing plan. 

Your API key should be sent inside the header with the parameter name ‘x-elasticemail-apikey’ and your API key as a value.

Installation and Usage

Installation

Installation

Our official downloadable C# library is available on GitHub.

Generate the DLL using your preferred tool (e.g. dotnet build)

Then include the DLL (under the bin folder) in the C# project, and use the namespaces:

Copied!
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Usage

To use the API client with a HTTP proxy, setup a System.Net.WebProxy

Copied!
Configuration c = new Configuration();
System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/");
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
c.Proxy = webProxy;

To get started, put the following lines:

Copied!
using System.Collections.Generic;
using System.Diagnostics;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;
namespace Example
{
    public class Example
    {
        public static void Main()
        {
            Configuration config = new Configuration();
            config.BasePath = "https://api.elasticemail.com/v4";
            // Configure API key authorization: apikey
            config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");
            // Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
            // config.ApiKeyPrefix.Add("X-ElasticEmail-ApiKey", "Bearer");
            var apiInstance = new CampaignsApi(config);
            var name = "name_example";  // string | Name of Campaign to delete
            try
            {
                // Delete Campaign
                apiInstance.CampaignsByNameDelete(name);
            }
            catch (ApiException e)
            {
                Debug.Print("Exception when calling CampaignsApi.CampaignsByNameDelete: " + e.Message );
                Debug.Print("Status Code: "+ e.ErrorCode);
                Debug.Print(e.StackTrace);
            }
        }
    }
}

Quick start guide

In this section, we will tell you the steps to start sending emails with our email API.

1. Register a free account

Register and activate your Elastic Email account to get access to our product.

2. Verify your domain

Follow the instructions on our settings page to verify your domain and start sending with us.

3. Create an API Key

Go to your setting to generate an API Key to add to your code later.

4. Install our libraries

Install our C# library as explained in the Installation and usage section.

5. Send your first email with API

Now it’s time to send your first test email with API to make sure that everything is set up correctly after downloading the library and the authentication process.

Code Samples

Once you have sent the test email with our API, you can start sending transactional emails and perform various operations directly through your app. We have prepared for you some code samples for most essential actions, including managing contacts and lists, creating templates, and sending and tracking emails. Thanks to them, your integration with our API will be an even easier and more pleasant experience.
In case any of the following snippets are changed in the meantime, the most recent version of this API Library is available on GitHub.

Managing Contacts

In this section, we will walk you through managing the contacts on your account using the C# library.

Add Contacts

To add contacts to your account, you will need Access Level: ModifyContacts.

Put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all contact's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of ContactsApi that will be used to work with contact's methods.

Copied!
var apiInstance = new ContactsApi(config);

Create an array with new contacts. You can pass an array with up to 1000 contacts. The Email field is mandatory, the rest is optional.

Find out more by checking our API's documentation.

Copied!
List<ContactPayload> myContacts = new List<ContactPayload>();
ContactPayload singleContact = new ContactPayload(email: "johnsmith@domain.com");
singleContact.FirstName = "John";
singleContact.LastName = "Smith";
myContacts.Add(singleContact);

Specify an existing list name in options, otherwise contacts will be added to all contacts.

Copied!
List<string> listnames = new List<string>();
listnames.Add("yourList");

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, we will return the data that API sends us about the newly added contact. Finally, call the API to add a contact:

Copied!
try
{
    return apiInstance.ContactsPost(myContacts, listnames);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling ContactsApi.ContactsPost: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Delete contacts

If you want to delete a contact, you will need Access Level: ModifyContacts.

To delete a contact, put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all contact's methods: Generate and use your API key (remember to check a required access level)

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of ContactsApi that will be used to work with contact's methods.

Copied!
var apiInstance = new ContactsApi(config);

Create an object with an array of contacts to delete.

Find out more by checking our API's documentation.

Copied!
EmailsPayload emails = new EmailsPayload();
emails.Emails = new List<string>();
emails.Emails.Add("johnsmith@domain.com");

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a success message. Finally, call the API to delete contact:

Copied!
try
{
    apiInstance.ContactsDeletePost(emails);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling ContactsApi.ContactsExportPost: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Export Contacts

If you want to export the selected contact to a downloadable file, you will need Access Level: Export.

Put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all contact's methods: Generate and use your API key (remember to check a required access level)

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of ContactsApi that will be used to work with contact's methods.

Copied!
var apiInstance = new ContactsApi(config);

In this example we will export contacts to a CSV file. Create options:

  • fileFormat - specify format in which file should be created, options are: "Csv" "Xml" "Json".
  • emails - select contacts to export by providing array of emails
  • fileName - you can specify file name of your choice

Other options:

  • rule - eg. rule=Status%20=%20Engaged – Query used for filtering
  • compressionFormat - "None" "Zip"

Find out more by checking our API's documentation.

Copied!
ExportFileFormats fileFormat = new ExportFileFormats();
fileFormat = ExportFileFormats.Csv;
List<string> emails = new List<string>();
emails.Add("johnsmith@domain.com");
string fileName = "exported.csv";

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a success messages and link to a file. Finally, call the API to export contacts:

Copied!
try
{
    apiInstance.ContactsExportPost(fileFormat, null, emails, null, fileName);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling ContactsApi.ContactsExportPost: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Managing Lists

In this section, we will walk you through managing your contact list on your account using the C# library.

Add List

To add a list, you will need Access Level: ModifyContacts.

Put the following code into your file. 

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all list's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of ListsApi that will be used to work with list's methods.

Copied!
var apiInstance = new ListsApi(config);

Create an object with details about new list. Only ListName is required. You can also define if to allow unsubscription from list and pass an emails array of existing contacts on your account to add them to list during list creation.

Find out more by checking our API's documentation.

Copied!
ListPayload listData = new ListPayload(listName: "My contacts");
listData.AllowUnsubscribe = true;
listData.Emails = new List<string>();
listData.Emails.Add("johnsmith@domain.com");

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a success messages. Finally, call the API to add list:

Copied!
try
{
    return apiInstance.ListsPost(listData);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling ListsApi.ListsPost: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Load List

To load a list, you will need Access Level: ViewContacts.

Put the following code into your file. 

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all list's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of ListsApi that will be used to work with list's methods.

Copied!
var apiInstance = new ListsApi(config);

The only thing needed is a list name.

Find out more by checking our API's documentation.

Copied!
string listName = "My contacts";

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a list object. Finally, call the API to load a list:

Copied!
try
{
    return apiInstance.ListsByNameGet(listName);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling ListsApi.ListsByNameGet: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Delete List

To remove a contact list from your account, you will need Access Level: ModifyContacts.

Put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all list's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of ListsApi that will be used to work with list's methods.

Copied!
var apiInstance = new ListsApi(config);

The only thing needed is a list name.

Find out more by checking our API's documentation.

Copied!
string listName = "My contacts";

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a success messages. Finally, call the API to delete a list:

Copied!
try
{
    apiInstance.ListsByNameDelete(listName);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling ListsApi.ListsByNameDelete: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Creating Templates

An email template is a body of email prepared and saved under a given name. In this section, you will get to know how to add and load email templates.

Add Template

To add a template, you will need Access Level: ModifyTemplates.

Put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all template's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of TemplatesApi that will be used to work with list's methods.

Copied!
var apiInstance = new TemplatesApi(config);

Create an object with details about new template:

  • Name – name of your template by which it can be identified and used
  • Subject – specify default subject for this template
  • Body – specify acctual body content eg. in HTML, PlainText or both
  • TemplateScope – specify scope, "Personal" template won't be shared, "Global" template can be shared with your sub accounts.

Find out more by checking our API's documentation.

Copied!
TemplatePayload template = new TemplatePayload(name: "My new template");
template.Subject = "Default Subject";
template.TemplateScope = TemplateScope.Personal;
template.Body = new List<BodyPart>();
BodyPart bodyPart = new BodyPart();
bodyPart.ContentType = BodyContentType.HTML;
bodyPart.Charset = "utf-8";
bodyPart.Content = "<h1>My template</h1>";
template.Body.Add(bodyPart);

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display object with newly created template data. Finally, call the API to add template:

Copied!
try
{
    return apiInstance.TemplatesPost(template);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling TemplatesApi.TemplatesPost: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Load Template

To load existing template details, you will need Access Level: ViewTemplates.

Put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all template's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of TemplatesApi that will be used to work with list's methods.

Copied!
var apiInstance = new TemplatesApi(config);

To load a template you need to specfiy it's name.

Find out more by checking our API's documentation.

Copied!
string templateName = "My template";

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a template object. Finally, call the API to load a template:

Copied!
try
{
    return apiInstance.TemplatesByNameGet(templateName);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling TemplatesApi.TemplatesByNameGet: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Delete Template

To delete a template, you will need Access Level: ModifyTemplates.

Put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all template's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of TemplatesApi that will be used to work with list's methods.

Copied!
var apiInstance = new TemplatesApi(config);

The only thing needed is a template name.

Find out more by checking our API's documentation.

Copied!
string templateName = "My contacts";

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a success messages. Finally, call the API to delete a template:

Copied!
try
{
    apiInstance.TemplatesByNameDelete(templateName);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling TemplatesApi.TemplatesByNameGet: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Sending Emails

In this section, we will tell you how you can start sending emails using the Javascript library. You will get to know how to send bulk and transactional emails.

Send Transactional Emails

To send transactional emails, you will need Access Level: SendHttp.

Put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all email's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of EmailsApi that will be used to work with email's methods.

Copied!
var apiInstance = new EmailsApi(config);

Next, you need to specify email details:

  • email recipients
  • email content:
    • body parts – in HTML, PlainText or in both
    • from email – it needs to be your validated email address
    • email subject

Find out more by checking our API's documentation.

Copied!
var to = new List<string>();
to.Add("johnsmith@domain.com");
var recipients = new TransactionalRecipient(to: to);
EmailTransactionalMessageData emailData = new EmailTransactionalMessageData(recipients: recipients);
emailData.Content = new EmailContent();
emailData.Content.Body = new List<BodyPart>();
BodyPart htmlBodyPart = new BodyPart();
htmlBodyPart.ContentType = BodyContentType.HTML;
htmlBodyPart.Charset = "utf-8";
htmlBodyPart.Content = "<h1>Mail content</h1>";
BodyPart plainTextBodyPart = new BodyPart();
plainTextBodyPart.ContentType = BodyContentType.PlainText;
plainTextBodyPart.Charset = "utf-8";
plainTextBodyPart.Content = "Mail content";
emailData.Content.Body.Add(htmlBodyPart);
emailData.Content.Body.Add(plainTextBodyPart);
emailData.Content.From = "myemail@domain.com";
emailData.Content.Subject = "Example transactional email";

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a success messages. Finally, call the API to send email:

Copied!
try
{
    return apiInstance.EmailsTransactionalPost(emailData);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling EmailsApi.EmailsTransactionalPost: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Send Bulk Emails

Put the following code to your file to send a bulk email, meaning a single email sent to a large group at once.

You will need Access Level: SendHttp.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all email's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of EmailsApi that will be used to work with email's methods.

Copied!
var apiInstance = new EmailsApi(config);

Next, you need to specify email details:

  • email recipients:
    • this example demostrates merge fields usage, for each recipient {name} will be changed to recipient's name
  • email content:
    • body parts – in HTML, PlainText or in both
    • from email – it needs to be your validated email address
    • email subject

Find out more by checking our API's documentation.

Copied!
List<EmailRecipient> emailRecipients = new List<EmailRecipient>();
var recipients = new EmailRecipient(email: "johnsmith@domain.com");
recipients.Fields = new Dictionary<string, string>();
recipients.Fields.Add("name", "John");
emailRecipients.Add(recipients);
EmailMessageData emailData = new EmailMessageData(recipients: emailRecipients);
emailData.Content = new EmailContent();
emailData.Content.Body = new List<BodyPart>();
BodyPart htmlBodyPart = new BodyPart();
htmlBodyPart.ContentType = BodyContentType.HTML;
htmlBodyPart.Charset = "utf-8";
htmlBodyPart.Content = "<h1>Mail content</h1>";
BodyPart plainTextBodyPart = new BodyPart();
plainTextBodyPart.ContentType = BodyContentType.PlainText;
plainTextBodyPart.Charset = "utf-8";
plainTextBodyPart.Content = "Mail content";
emailData.Content.Body.Add(htmlBodyPart);
emailData.Content.Body.Add(plainTextBodyPart);
emailData.Content.From = "myemail@domain.com";
emailData.Content.Subject = "Example email";

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a success message. Finally, call the API to delete contact:

Copied!
try
{
    return apiInstance.EmailsPost(emailData);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling EmailsApi.EmailsPost: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Managing Campaigns

Add Campaign

To add your first campaign using the C# library, you will need Access Level: ModifyCampaigns. Mind that when using Elastic Email, when you send an email to any group of contacts, we call it a “campaign”.

To add a campaign, put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all campaign's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of CampaignsApi that will be used to create a campaign.

Copied!
CampaignsApi apiInstance = new CampaignsApi(config);

Create an example campaign object:

  • Name: defines campaign name by which you can identify it later
  • Recipients: define your audience
  • Conent: define your message details
  • Status: define status in which campaign should be created

Find out more by checking our API's documentation.

Send will be triggered immediately or postponed, depending on given options. Because we define Status as Draft, so in this case it will be postponed and campaign will be added to drafts.

Copied!
CampaignRecipient recipients = new CampaignRecipient();
recipients.ListNames = new List<string>();
recipients.ListNames.Add("your list");
Campaign yourCampaign = new Campaign(name: "My Campaign", recipients: recipients);
yourCampaign.Content = new List<CampaignTemplate>();
CampaignTemplate campaignTemplate = new CampaignTemplate();
campaignTemplate.From = "myemail@domain.com";
campaignTemplate.ReplyTo = "myemail@domain.com";
campaignTemplate.TemplateName = "hello_template";
campaignTemplate.Subject = "Hello";
yourCampaign.Content.Add(campaignTemplate);
yourCampaign.Status = CampaignStatus.Draft;

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, we will return the data that API sends us about the newly created campaign. Finally, call the API to create a campaign:

Copied!
try
{
    return apiInstance.CampaignsPost(yourCampaign);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling CampaignsApi.CampaignsPost: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Load Campaign

To load details about a campaign list in your account using the C# library, you will need Access Level: ViewCampaigns. Mind that when using Elastic Email, when you send an email to any group of contacts, we call it a “campaign”.

To load a campaign, put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all campaign's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of CampaignsApi that will be used to create a campaign.

Copied!
CampaignsApi apiInstance = new CampaignsApi(config);

Create variable with campaign's name which you want to load

Copied!
string campaignName = "My Campaign";

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, we will return the data that API sends us about the loaded campaign. Finally, call the API to load a campaign:

Copied!
try
{
    return apiInstance.CampaignsByNameGet(campaignName);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling CampaignsApi.CampaignsByNameGet: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Update Campaign

To update existing campaigns in your account using the C# library, you will need Access Level: ModifyCampaigns. Mind that when using Elastic Email, when you send an email to any group of contacts, we call it a “campaign”.

To update a campaign, put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all campaign's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of CampaignsApi that will be used to create a campaign.

Copied!
CampaignsApi apiInstance = new CampaignsApi(config);

Create an example campaign object:

  • Name: defines campaign name by which you can identify it later
  • Recipients: define your audience
  • Conent: define your message details
  • Status: define status in which campaign should be created

Find out more by checking our API's documentation. 

Send will be triggered immediately or postponed, depending on given options. Because we define Status as Draft, so in this case it will be postponed and campaign will be added to drafts.

Copied!
var recipients = new CampaignRecipient();
recipients.ListNames = new List<string>();
recipients.ListNames.Add("yourList");
string campaignName = "My Campaign";
Campaign yourCampaign = new Campaign(name: "new campaignName", recipients: recipients);
yourCampaign.Content = new List<CampaignTemplate>();
CampaignTemplate campaignTemplate = new CampaignTemplate();
campaignTemplate.From = "myemail@domain.com";
campaignTemplate.ReplyTo = "myemail@domain.com";
campaignTemplate.TemplateName = "hello_template";
campaignTemplate.Subject = "Hello";
yourCampaign.Content.Add(campaignTemplate);
yourCampaign.Status = CampaignStatus.Draft;

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, we will return the data that API sends us about the updated campaign. Finally, call the API to update a campaign:

Copied!
try
{
    return apiInstance.CampaignsByNamePut(campaignName, yourCampaign);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling CampaignsApi.CampaignsByNamePut: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Delete Campaign

To delete an existing campaign, you will need Access Level: ModifyCampaigns. Mind that when using Elastic Email, when you send an email to any group of contacts, we call it a “campaign”.

To delete a campaign, put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all campaign's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of CampaignsApi that will be used to create a campaign.

Copied!
CampaignsApi apiInstance = new CampaignsApi(config);

Create a variable with campaign's name which you want to delete

Copied!
string campaignName = "My Campaign";

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a success messages. Finally, call the API to delete a campaign:

Copied!
try
{
    apiInstance.CampaignsByNameDelete(campaignName);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling CampaignsApi.CampaignsByNameDelete: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Tracking

In this section, we will walk you through the steps of managing actions related to email tracking. You will get to know how to load delivery and campaign statistics from your account using the C# library.

Load Statistics

To load delivery statistics from your account, you will need Access Level: ViewReports.

Put the following code into your file.

Load library using the line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all statistic's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of StatisticsApi that will be used to work with statistic's methods.

Copied!
var apiInstance = new StatisticsApi(config);

First you need to specify a date range:

  • from date
  • to date – optional

Find out more by checking our API's documentation. 

Copied!
DateTime from = new DateTime(2022, 01, 17);
DateTime to = DateTime.Today;

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display a statistic data. Finally, call the API to load statistics:

Copied!
try
{
    return apiInstance.StatisticsGet(from, to);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling StatisticsApi.StatisticsGet: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Load Channels Statistics

To load statistics for each channel from your account, you will need Access Level: ViewChannels.

Put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all statistic's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of StatisticsApi that will be used to work with statistic's methods.

Copied!
var apiInstance = new StatisticsApi(config);

Channels statistics reponse is paginated you need to specfiy pagination options:

  • limit – maximum returned items, limit = 0 means to return everything till the end of the list
  • offset – how many items should be skipped from begging

Eg. to return first 20 elements specify pagination options as follows

Find out more by checking our API's documentation.

Copied!
int limit = 20;
int offset = 0;

Let's fetch everything:

Copied!
int limit = 0;
int offset = 0;

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display statistics data. Finally, call the API to load channels statistics:

Copied!
try
{
    return apiInstance.StatisticsChannelsGet(limit, offset);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling StatisticsApi.StatisticsChannelsGet: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Load Campaigns Stats

To load statistics for each email campaign from your account, you will need Access Level: ViewChannels.

Put the following code into your file.

Load library using the following line:

Copied!
using System;
using System.Collections.Generic;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;

Below code you will repeat in all statistic's methods: Generate and use your API key (remember to check a required access level):

Copied!
Configuration config = new Configuration();
// Configure API key authorization: apikey
config.ApiKey.Add("X-ElasticEmail-ApiKey", "YOUR_API_KEY");

Create an instance of StatisticsApi that will be used to work with statistic's methods.

Copied!
var apiInstance = new StatisticsApi(config);

Campaigns statistics reponse is paginated you need to specfiy pagination options:

  • limit – maximum returned items, limit = 0 means to return everything till the end of the list
  • offset – how many items should be skipped from begging

Eg. to return first 20 elements specify pagination options as follows:

Copied!
int limit = 20;
int offset = 0;

Let's fetch everything:

Copied!
int limit = 0;
int offset = 0;

Find out more by checking our API's documentation.

Surround the api call in the try catch block, so that in case of error it will display error details. Otherwise, it will display statistics data. Finally, call the API to load campaigns statistics:

Copied!
try
{
    return apiInstance.StatisticsCampaignsGet(limit, offset);
}
catch (ApiException e)
{
    Console.WriteLine("Exception when calling StatisticsApi.StatisticsCampaignsGet: " + e.Message);
    Console.WriteLine("Status Code: " + e.ErrorCode);
    Console.WriteLine(e.StackTrace);
    throw;
}

Ready to get started?

Tens of thousands of companies around the world already send their emails with Elastic Email.
Join them and discover your own email superpowers.

Set up in 2 minutes

No credit card required