Email API Integration Guide with C#

Minimum code that you need to integrate Mailazy with your .Net application


Prerequisites

You need to complete these given prerequisites, you can skip the step if you have already completed.

  1. Sign up for a Mailazy account.
  2. Complete Domain Authentication.
  3. Generate the Mailazy Access Key

Integrate Mailazy with C#

Start Sending mail using the code provided below:

var client = new RestClient("https://api.mailazy.com/v1/mail/send");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("X-Api-Key", "<API KEY>");
request.AddHeader("X-Api-Secret", "<API SECRET>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{" +
      "\"to\": [\"example@example.com\"]," +
      "\"from\": \"Sender <sender@example.com>\"," +
      "\"subject\": \"<Subject>\"," +
      "\"content\": [{\"type\": \"text/plain\",\"value\": \"<Content>\"}]" +
    "}",  ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

or alternatively if you want to use HttpClient:

// Request Payload
public class MailazyEmailRequestPayload
{
  public string[] To { get; set; }
  public string From { get; set; }
  public string Subject { get; set; }
  public MailazyEmailRequestContent[] Content { get; set; }
}

public class MailazyEmailRequestContent
{
  public string Type { get; set; }
  public string Value { get; set; }
}

// Send email
var requestPayload = new MailazyEmailRequestPayload
{
    From = "from@example.com",
    To = new[] { "to@example.com" },
    Subject = "test",
    Content = new[]
    {
        new MailazyEmailRequestContent
        {
            Type = "text/plain",
            Value = "hello world"
        },
        new MailazyEmailRequestContent
        {
            Type = "text/html",
            Value = "<b>hello world<b/>"
        },
    }
};

using var client = new HttpClient();
client.BaseAddress = new Uri("https://api.mailazy.com");
client.DefaultRequestHeaders.Add("x-api-key", "<api-key-here>");
client.DefaultRequestHeaders.Add("x-api-secret", "<api-secret-here>");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/v1/mail/send");
var json = Newtonsoft.Json.JsonConvert.SerializeObject(requestPayload);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");

var result = await client.SendAsync(request);
string resultContent = await result.Content.ReadAsStringAsync();
Console.WriteLine(resultContent);