Skip to content

Instantly share code, notes, and snippets.

@ronnieoverby
Created March 31, 2016 13:30
Show Gist options
  • Save ronnieoverby/bdde449e5d1185e74f81001625f6f05f to your computer and use it in GitHub Desktop.
Save ronnieoverby/bdde449e5d1185e74f81001625f6f05f to your computer and use it in GitHub Desktop.
using System.Net.Http; // reference System.Net.Http.dll
using System.Net.Http.Headers;
using System.Threading.Tasks;
public static class MailgunSample
{
public static async Task<string> SendSimpleMessageAsync(string domain, string apikey, string from, string to, string subject, string text)
{
var authHeader = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes($"api:{apikey}")));
var requestContent = new FormUrlEncodedContent(new Dictionary<string, string>
{
["from"] = from,
["to"] = to,
["subject"] = subject,
["text"] = text
});
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.mailgun.net/v3/");
client.DefaultRequestHeaders.Authorization = authHeader;
using (var response = await client.PostAsync($"{domain}/messages", requestContent))
return await response.Content.ReadAsStringAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment