how to integrate constantcontact with .net application (C#)

Regular Participant

how to integrate constantcontact with .net application (C#)

 Hi All,


I need to send emails to the customers using a constant conatact API.How do i start with? I have registered to the developer site. What is next step to start with? It would be great ,if any one provide the step by step information.

Thanks,


siddhuoops

 The Bladerunner

6 REPLIES 6
Employee

Hello,



Our API definetly has the ability to send emails through Constant Contact.  Currently all customers that have an API key can create custom HTML and XHTML emails with our API using the Campaign Collection API.  In order to schedule these emails through the API so that they are sent out, you do need to get this added to your API key by requesting it from the AppConnect team.



If you have any questions about this please let me know.



Regards,

Benjamin Soder
NOC Analyst
Constant Contact
Regular Participant

 Thanks for the reply. What i want to know is how this is going to work. The way i need to set up is we get a list from the customer weekly. That list includes customer's email, contact information. Since this has to be automated, i am setting up a job in sql server that will load the spreadsheet into the database. Using the database, i will be then adding the customers' info on constant and contact via the API. So, i will have to create a email campaign inside constant and contact. Using my app, I will have to upload a list through the automated process in this campaign. 


An email template has to be created inside cons and contact that will get the variable fields from the list. And I assume that campaign then can be scheduled to run weekly. The .Net application will have to feed the fresh data every week to the same place where this template is going to get the data from. Please correct me if I am saying something that can't be done.


I just want to get the idea clear before i start programming my piece. Will this even work?

 The Bladerunner

Regular Participant

 


public string Bulk_POST(string APIKey, string uName, string pWord)


    {


        CredentialCache LoginCredentials = new CredentialCache();


        LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/USERNAME" + uName), "Basic", new NetworkCredential(APIKey + '%' + uName, pWord));


        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact/com/ws/customers/" +


            uName + "/activities");


        request.Method = "POST";


        request.ContentType = "application/x-www-form-urlencoded";


        request.Credentials = LoginCredentials;


        sListUri = "https://api.constantcontact.com/ws/customers/" + uName + "/lists/TestContact";


        StringBuilder data = new StringBuilder();


        data.Append("activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8));


        data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,Email Type,First Name,Last Name,Company Name" + "\n"), Encoding.UTF8));


        //get the data from db and append that to the stringbuilder


        SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings.ConnectionString);


        SqlCommand scmd = new SqlCommand("SELECT * FROM CustomerSurveyTemp WHERE IS NOT NULL", scon);


        SqlDataReader sdr;


        try


        {


            scon.Open();


            sdr = scmd.ExecuteReader();


            sdr.Read();


            data.Append(HttpUtility.UrlEncode((sdr.ToString() + ",HTML," + sdr.ToString() + "," + sdr.ToString() + "," + "SA"), Encoding.UTF8));


        }


        catch (Exception ex)


        {


            throw new Exception("Error occured: " + ex);


        }


        finally


        {


            scon.Close();


        }


        data.Append("&lists=" + HttpUtility.UrlEncode(sListUri));


 


        byte[] byteArray = Encoding.UTF8.GetBytes(data.ToString());


        try


        {


            request.ContentLength = byteArray.Length;


            string XMLResponse = "Bytes to send: " + byteArray.Length;


            Stream streamRequest = request.GetRequestStream();


            streamRequest.Write(byteArray, 0, byteArray.Length);


            streamRequest.Close();


            HttpWebResponse Response = (HttpWebResponse)request.GetResponse();


            StreamReader Reader = new StreamReader(Response.GetResponseStream());


            XMLResponse += Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();


            Reader.Close();


            Response.Close();


            return XMLResponse;


        }


        catch (WebException e)


        {


            return ("WebException: " + e.Status + "With response: " + e.Message);


        }


 


        catch (Exception e)


        {


            return ("Exception: " + e.Message);


        }


    }


 


I assume this will help me in adding multiple emails to an email campaign called TestList. Does this look complete?


 

 The Bladerunner

Employee

Email addresses are attached to Contact Lists rather than actual Email Campaigns. The code you have here looks complete with the exception of a few things:


1. A list must be referenced by its ID and not its name, for example, https://api.constantcontact.com/ws/customers/{user-name}/lists/1


2. The URI you are using in your WebRequest.Create is 'https://api.constantcontact/com/ws/customers/"', you would want to change this to "https://api.constantcontact.com/ws/customers/"


Other than that, as long as you have the specific list ID you are using assigned to the campaign you're looking to send this should successfully attach contacts to that particular list (and thus they will receive the campaign so long as that campaign is set to receive the desired email.


 


I hope this helps. Please let me know if you have any questions or concerns regarding this. Thanks.

David J

Regular Participant

Hi David,


Thanks a bunch for your reply. 


So, here https://api.constantcontact.com/ws/customers/{user-name}/lists/1, where would the name of the email campaign go? 


If I create an email campaign called EmailBlast, do I need to put this name anywhere? We have several email campaigns that exist on constant and contact right now. So, by putting in just ID, will the contacts get added to the right list then?

 The Bladerunner

Employee

'Contacts' are added to specific 'Lists'. (See Creating a Contact). In the XML that represents the contact, there is a section called <ContactLists> that will contain a <ContactList> element for each list the subscriber is going to be subscribed too. You can also update a contact to adjust their Contact List subscriptions (See Adding or Removing a Contact from a List). However, for the bulk activity you posted above, this is the section that says: 'data.Append("&lists=" + HttpUtility.UrlEncode(sListUri));'


 

When creating a campaign, you would then decide which ContactLists are going to receive that specific campaign. (See Creating a Campaign). The Campaign also has a section called ‘<ContactLists>’ which contains a specific <ContactList> element for each list the campaign is meant to be delivered too.

 

Obviously creating contacts through a bulk activity is a little different, but as long as the contact is being added to a list that is included in the <ContactLists> section for a campaign, that user will receive that campaign once it is scheduled.

David J

Developer Portal

View API documentation, code samples, get your API key.

Visit Page