Created
September 24, 2012 18:38
-
-
Save duncansmart/3777530 to your computer and use it in GitHub Desktop.
Send a mail message via Mailgun using HttpClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] = "[email protected]"; | |
form["to"] = "[email protected]"; | |
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