Using Cake Mail service through API by C#

Let’s get started from question what is Cake Mail? Cake Mail is service that gives you ability to manage all subscribers on your site and sending emails to your subscribers. So I want to tell you how we can work with Cake Mail service through their API. But first, I want to pay attention to the fact that exist two resources which provide cake mail services this and thisagree with you, sounds confusing. I won’t be describe difference between first cake mail and second, just tell that service which provided by Cloudtools Rackspace has much more functionality (checked). So, let’s get started, and in this post I’m going to show how to use cake mail service from Cloudtools Rackspace using simple C# console application.

NOTE: If you are going to use cake mail from this vendor, then current example would be not so correct, only difference is that you should first to get user_key from first response, and after that you can be able to make any queries.

First of all, you should ask Cake Mail folks to give you next specific data: API KEY and USER KEY. If you have it already, great.  

Goal of this test console application is a moving all subscribers from first list to the second one by special date.

Code from Program.cs

        ///<summary>

        /// Mains the specified args.

        ///</summary>

        ///<param name=”args”>The args.</param>

        static void Main(string[] args)

        {

            Console.WriteLine(string.Format(“{0:d/M/yyyy HH:mm:ss} Started”, “\n\n\n” + DateTime.Now));

            string login = ConfigurationManager.AppSettings[“CakeMailLogin”];

            string password = ConfigurationManager.AppSettings[“CakeMailPassword”];

            string apiKey = ConfigurationManager.AppSettings[“CakeMailApiKey”];

            string userKey = ConfigurationManager.AppSettings[“CakeMailUserKey”];

            CakeMailManager cakeMailManager = new CakeMailManager();

            cakeMailManager.ApiKey = apiKey;

            cakeMailManager.UserKey = userKey;

           if (!string.IsNullOrEmpty(login)

                && !string.IsNullOrEmpty(password)

                && !string.IsNullOrEmpty(apiKey)

                && !string.IsNullOrEmpty(userKey))

            {

                int tempListId;

                int.TryParse(ConfigurationManager.AppSettings[“CakeMailHoldingListId”], out tempListId);

                int mainListId;

                int.TryParse(ConfigurationManager.AppSettings[“CakeMailMainListId”], out mainListId);

                DateTime fromDate = DateTime.Now.AddDays(-14);

                DateTime toDate = DateTime.Now.AddDays(-7);

                Console.WriteLine(“Get all subscribed emails from \”first list\” from “ + fromDate + ” to “ + toDate);

                // Get all subscribed emails from “first list”

                List<CakeMailMember> cakeMailMembersList = cakeMailManager.GetMembersByPeriod(tempListId, fromDate, toDate);

                Console.WriteLine(“Was obtained: “ + (cakeMailMembersList != null ? cakeMailMembersList.Count : 0) + ” emails”);

                if (cakeMailMembersList != null && cakeMailMembersList.Count > 0)

                {

                    Console.WriteLine(“Begin to move into the second list”);

                    //Delete all members from first list

                    cakeMailManager.DeleteMembersFromList(cakeMailMembersList, tempListId);

                    // Move these members to second list

                    cakeMailManager.AddMembersToList(cakeMailMembersList, mainListId, false, false);

                    Console.WriteLine(“Moving has been completed.”);

                }

            }

            Console.WriteLine(string.Format(“{0:d/M/yyyy HH:mm:ss} Ended \r\n”, DateTime.Now));

        }

 

(I won’t be write all code here, just attached it at the end of article)

In the main method happens next:

1) We are getting all emails from first list by period:

cakeMailManager.GetMembersByPeriod(tempListId, fromDate, toDate);

2) Then we are deleting all subscribers from first list

cakeMailManager.DeleteMembersFromList(cakeMailMembersList, tempListId);

3) Saving all subscribers to the second list

cakeMailManager.AddMembersToList(cakeMailMembersList, mainListId, false, false);

 

All needed parameters are located in the App.config file

<?xml version=1.0” encoding=utf-8 ?>

<configuration>

  <appSettings>

    <!– Cake Mail –>

    <add key=CakeMailLogin” value=XXXX@XXXX.XXXX/>

    <add key=CakeMailPassword” value=XXXX/>

    <add key=CakeMailApiKey” value=XXXX/>

    <!– User key –>

    <add key=CakeMailUserKey” value=XXXX/>

    <!– First list –>

    <add key=CakeMailHoldingListId” value=XXXX/>

    <!– Second list –>

    <add key=CakeMailMainListId” value=XXXX/>

  </appSettings>

</configuration>

 

For getting the ID of the list you may simply click by the list and take a look on the URL address

List ID of Cake Mail

 

 

also was created WebPostRequest class that help us to make POST queries and all needed urls addresses are stored in the Const class

code from Const.cs

 

    ///<summary>

    /// Const

    ///</summary>

    public static class Const

    {

        ///<summary>

        /// Method to check the login of a user.

        ///</summary>

        public const string URL_CAKE_MAIL_USER_INFO = “https://api.wbsrvc.com/User/login”;

        ///<summary>

        /// Method to add a member to the list.

        ///</summary>

        public const string URL_CAKE_MAIL_SUBSCRIBE_EMAIL = “https://api.wbsrvc.com/List/SubscribeEmail/”;

        ///<summary>

        /// Method to get a list of members of the list.

        ///</summary>

        public const string URL_CAKE_MAIL_LIST_SHOW = “https://api.wbsrvc.com/List/Show/”;

        ///<summary>

        /// Method to delete a member from the list.

        ///</summary>

        public const string URL_CAKE_MAIL_DELETE_RECORD = “https://api.wbsrvc.com/List/DeleteRecord/”;

    }

 

For more details about queries you have to take a look at the API documentation here

NOTE: Be careful with quotes in the query, they should be the same as in documentation, this is not simple quotes, please take a look at the figure:

quotes

Also I’ve created set of classes which helps to convert json response to the C# entities:

 

            JavaScriptSerializer ser = new JavaScriptSerializer();

            string jsonResult = postQuery.GetResponse();

            //Get JSON response and convert it to entity

            CakeMailMembersListFeed cakeMailMembersListFeed = ser.Deserialize<CakeMailMembersListFeed>(jsonResult);

List of classes which helps to convert json response:

CakeMailUser.cs

CakeMailMembersListFeed.cs

CakeMailMemberListEntry.cs

CakeMailMember.cs

CakeMailFeed.cs

and CakeMailManager have main methods for working with API, see attached.

That’s it

Attached file with source code: CakeMailService.zip (Visual Studio 2012)