Imnica Mail API Integration – ASP.NET 4.0 C#

Posted on Feb 9, 2012 | 0 comments

Ah, apis. You love them. But don’t they just suck when the documentation is awful? Yeah, welcome to Imnica Mail’s API documentation.

First, the URL is wrong. The base should be http://www.imnicamail.com/v4/api.php – not what they have listed.

Second, if you use the JSON result, they add a “true(” to the beginning and “)” at the end (both without quotes). Again, no where in their documentation.

Third, you need the List ID, not the name. While this is mentioned, it’s confusing and sorta hard to find if you’re not technically inclined. Here’s how you find it. Navigate to you list and then grab it off the url. Show in this screenshot:

Imnica Mail List ID

 

So, I thought I’d try and save you some time and give you the code I used to integrate as I didn’t find any when I googled.

Here it is. You’ll need to get a JSON deserializer, I used SimpleJson which you can install with NuGet.

One more note: This will return true if the user is a new subscriber or already subscribed.

//API Reference: http://support.imnica.com/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=32&nav=0,4
private bool ImnicaSubscribe(string email, string listID)
{
    if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(listID))
    {
	return false;
    }

    //This variable is specific to Rackspace Cloud Sites, you may want to use another
    string ip = string.IsNullOrEmpty(Request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"]) ? "127.0.0.1" : Request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"].Trim().ToLower();

    string url = "http://www.imnicamail.com/v4/api.php";
    StringBuilder sb = new StringBuilder();
    sb.Append("Command=Subscriber.Subscribe&ResponseFormat=JSON&JSONPCallBack=true&ListID=");
    sb.Append(listID);
    sb.Append("&EmailAddress=");
    sb.Append(email);
    sb.Append("&IPAddress=");
    sb.Append(ip);

    string sret = string.Empty;

    try
    {
	WebClient client = new WebClient();
	client.Headers["Content-type"] = "application/x-www-form-urlencoded";
	byte[] bret = client.UploadData(url, "POST", System.Text.Encoding.UTF8.GetBytes(sb.ToString()));
	sret = System.Text.Encoding.ASCII.GetString(bret);

	//Imnicamail likes to wrap its JSON in a "true()" so let's remove it
	if (!string.IsNullOrEmpty(sret) && sret.Length > 7) 
	{
	    int l = sret.Length - 6;
	    sret = sret.Substring(5, l);
	}
	else
	{
	    throw new Exception("value returned is formatted incorrectly = " + sret);
	}

	//Deserialize the JSON
	dynamic a = SimpleJson.SimpleJson.DeserializeObject(sret);
	if (a["Success"])
	{
	     return true;
	}
	else if (!a["Success"] && a["ErrorCode"] == 9) //9 is already subscribed so it's ok
	{
	    return true;
	}
	else
	{
	    throw new Exception("Imnica Mail Exception - " + a.ErrorCode);
	}
    }
    catch (Exception ex)
    {   
	//log for manual investigation
    }

    return false;
}

Hope that helps! If you can make the code better, please feel free to leave it in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *