using System;
using System.IO;
using System.Text;

public static void Main()
{
	//-- send the emailmessage with emailcode of 100000 to --//
	//	an array of contacts posted in the body of the request--//
	//	and name the alert "My Test Alert" --//
	string my_url = "http://api.v4.swiftreach.com/api/Messages/Email/Send/MY%20Test%20Alert/100000";
	//-- our API KEY credentials --//
	string API_KEY = "12345";

	UriBuilder my_uri = new UriBuilder(my_url);
	my_uri.Port = 80;
	System.Net.HttpWebRequest MyRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(my_uri.Uri);
	//-- Create Email is a POST --//
	MyRequest.Method = "POST";
	//-- Append the API KEY header --//
	MyRequest.Headers.Add("SwiftAPI-Key", API_KEY);

	//-- build the XML to POST --//
    string create_email = "<ArrayOfContact>"
								+ "<Contact>"
                                + "    <EntityName>Sample One</EntityName>"
                                + "    <EntityGuid>a44667cf-e4e5-4f28-b4e2-948ba2065c09</EntityGuid>"
                                + "    <Email>alerts@swiftreach.com</Email>"
                                + "    <SMSNetwork>0</SMSNetwork>"
                                + "    <PagerNetwork>0</PagerNetwork>"
                                + "    <UserDefined>"
                                + "    <KeyValue><Key>FieldOne</Key><Value>foobar</Value></KeyValue>"
                                + "    <KeyValue><Key>FieldTwo</Key><Value>other</Value></KeyValue>"
                                + "    </UserDefined>"
                                + "</Contact>"
                                + "<Contact>"
                                + "    <EntityName>Sample Two</EntityName>"
                                + "    <EntityGuid>a44667cf-e4e5-4f28-b4e2-948ba2065c09</EntityGuid>"
                                + "    <Email>alerts@swiftreach.com</Email>"
                                + "    <SMSNetwork>0</SMSNetwork>"
                                + "    <PagerNetwork>0</PagerNetwork>"
                                + "    <UserDefined>"
                                + "    <KeyValue><Key>FieldOne</Key><Value>foobar</Value></KeyValue>"
                                + "    <KeyValue><Key>FieldTwo</Key><Value>other</Value></KeyValue>"
                                + "    </UserDefined>"
                                + "</Contact>"
                                + "</ArrayOfContact>";

	//-- POST the xml --//
	byte[] bytes = System.Text.Encoding.ASCII.GetBytes(create_email);
    MyRequest.ContentLength = bytes.Length;
    MyRequest.ContentType = "text/xml; encoding='utf-8'";
    Stream s = MyRequest.GetRequestStream();
    s.Write(bytes, 0, bytes.Length);
    s.Close();

	try
    {
        //-- get the response --//
		System.Net.HttpWebResponse MyResponse = (System.Net.HttpWebResponse)MyRequest.GetResponse();
        string message = String.Format("Received HTTP {0}", MyResponse.StatusCode);
        using (StreamReader sr = new StreamReader(MyResponse.GetResponseStream()))
        {
            //-- Post was successful --//
			string response_body = sr.ReadToEnd();
			System.Console.WriteLine("SUCCESS - " + response_body);
        }
    }
    catch (System.Net.WebException webEx)
    {        
        //-- Post failed --//
		using (StreamReader sr = new StreamReader(webEx.Response.GetResponseStream()))
        {
            string response_body = sr.ReadToEnd();
            string error_message = String.Format("Received HTTP {0} : {1}", webEx.Status.ToString(), response_body);
			System.Console.WriteLine("ERROR - " + error_message);
        }
    }
}