Madmimi C# Sample code

We use Madmimi (an awesome emailing service) to send emails to our 9Slides users programmatically. Though they have pretty good developer help with plenty of samples, they do not have .Net C# samples for any of their REST APIs. We wrote some code from scratch, and thought it would be good idea to share it with other .Net C# developers who might be interested. (It works for us, use it at own risk)

Sending Madmimi promotion

You can create HTML promotions using WYSIWYG editor provided by madmimi. These promotions can be sent programmatically to your users using madmimi mailer apis. Below is C# sample

var request = WebRequest.Create("https://api.madmimi.com/mailer");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

//compose and POST message via Madmimi

var data = Encoding.UTF8.GetBytes(String.Format("username=YourMadMimiEmailAddress&api_key=YourMadMimiApiKey&promotion_name={4}&recipient={1}&from={0}&reply_to=no-reply@YourCompany.com>&body={5}", fromEmail, toEmail, bccEmail, ccEmail, MailSubject, MailBody));
request.ContentLength = data.Length;

using(var requestStream = request.GetRequestStream())

{

requestStream.Write(data, 0, data.Length);

}

var webResponse = request.GetResponse();

Above would send an email to your user and would also user’s email ID in the mad mini system, if not already there.

Now, if  you are sending your first ever email to the user, many a times you would like to add that user and his first/last name in the madmimi system. Below is how you can do it using Madmimi list management APIs.

request = WebRequest.Create("https://api.madmimi.com/audience_lists/BetaSignUp/add");

request.Method = "POST";

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

data = Encoding.UTF8.GetBytes(String.Format("username=YourMadMimiEmailAddress&api_key=YourMadMimiApiKey&email={0}&first_name={1}&last_name={2}", to, firstName, lastName));

request.ContentLength = data.Length;

using (var requestStream = request.GetRequestStream())

{

requestStream.Write(data, 0, data.Length);

}

webResponse = request.GetResponse();

Very easy, isn’t it?

Feel free to use above code and add your comments and suggestions as comments below.

This entry was posted in Uncategorized. Bookmark the permalink.