Skip to content

Instantly share code, notes, and snippets.

@duncansmart
Created September 24, 2012 18:38
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save duncansmart/3777530 to your computer and use it in GitHub Desktop.
Save duncansmart/3777530 to your computer and use it in GitHub Desktop.
Send a mail message via Mailgun using HttpClient
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
[TestFixture]
public class MyClass
{
const string DOMAIN = "samples.mailgun.org";
const string API_KEY = "key-...";
[Test]
public void Test1()
{
Test1Async().Wait();
}
public async Task Test1Async()
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes("api" + ":" + API_KEY)));
var form = new Dictionary<string, string>();
form["from"] = "from@example.com";
form["to"] = "to@example.org";
form["subject"] = "Test";
form["text"] = "testing testing...";
var response = await client.PostAsync("https://api.mailgun.net/v2/" + DOMAIN + "/messages", new FormUrlEncodedContent(form));
if (response.StatusCode == HttpStatusCode.OK)
{
Debug.WriteLine("Success");
}
else
{
Debug.WriteLine("StatusCode: " + response.StatusCode);
Debug.WriteLine("ReasonPhrase: " + response.ReasonPhrase);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment