Problem
How do I send an email using the outMail API in C# .NET?
How do I send an email in C# .NET with a RESTful API?
Solution
The following example of code assumes you have already got a fully functional development environment, and you have working knowledge of C# and the .NET Libraries.
In your VS Solution you will need to add the NuGet package Newtonsoft.JSON.
Example code outmail-send.cs
/**
* outmail-send.cs
*
*/
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace outmail_send
{
class Program
{
private const string apiSecretKey = "YOUR_SECRET_API_KEY";
private const string apiUrl = "https://api.smtp-engine.com/outmail/v1/email/send";
static void Main(string[] args)
{
List<string> toAddress = new List<string>();
toAddress.Add("someone@example.com");
toAddress.Add("someone-else@example.com");
// Create the new Email Message
Dictionary<string, object> newMessage = new Dictionary<string, object>();
newMessage.Add("api_key", apiSecretKey);
newMessage.Add("to", toAddress);
newMessage.Add("sender", "me@example.com");
newMessage.Add("subject", "Sending via Outmail API");
newMessage.Add("text_body", "This is the text message body");
newMessage.Add("html_body", "<body><h1>This is the HTML message</h1></body>");
// Convert the Dictionary to Json
string newMessageJsonStr = JsonConvert.SerializeObject(newMessage, Formatting.Indented);
// Create HTTP resource
HttpClient outMail = new HttpClient();
// Add an Accept header for JSON format.
outMail.DefaultRequestHeaders.Add("ContentType", "application/json");
// Create the content
var content = new StringContent(newMessageJsonStr, Encoding.UTF8, "application/json");
// Do the POST
var response = outMail.PostAsync(apiUrl, content).Result;
// Check the result for a valid HTTP 200 success and process the returned result
// or handler the failure.
if (response.IsSuccessStatusCode)
{
var jsonResponseStr = response.Content.ReadAsStringAsync().Result;
dynamic jsonResponse = JsonConvert.DeserializeObject(jsonResponseStr);
if (jsonResponse.ContainsKey("data"))
{
Console.WriteLine("status = {0}", jsonResponse["status"]);
Console.WriteLine("Message id = {0}", jsonResponse["data"]["email_id"]);
}
else
{
Console.WriteLine("response = {0}", response);
Console.WriteLine("json response = {0}", jsonResponse);
}
}
else
{
// Get the Json Error response and convert it
var jsonResponseStr = response.Content.ReadAsStringAsync().Result;
dynamic jsonResponse = JsonConvert.DeserializeObject(jsonResponseStr);
Console.WriteLine("status = {0}", jsonResponse["status"]);
Console.WriteLine("message = {0}", jsonResponse["message"]);
Console.WriteLine("error = {0}", jsonResponse["errors"]);
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
//Dispose once all HttpClient calls are complete.
outMail.Dispose();
}
}
}
Summary of server details
Outgoing server |
mxXXXXXX.smtp-engine.com As provided in your signup email. |
Outgoing server protocol |
SMTP |
Outgoing server port |
25, 465, 587, 2525 or 8025 |
Authentication Type |
Basic Authentication, SSL and TLS supported |
Username |
As provided |
Password |
As provided |