Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sample code showing how to use the Mandrill API
/*
* Code/Mandrill.cs
* These are calls to the Mandrill email service.
* Reference: https://mandrillapp.com/api/docs/
*
* ajh 2012-08-09: new
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Dynamic;
using EasyHttp.Http;
using System.Net;
using log4net;
namespace Sample.Web
{
enum MandrillError
{
OK,
WebException,
HttpNotOk,
Invalid,
Rejected,
Unknown
}
public class Mandrill
{
static string MandrillBaseUrl = ConfigurationManager.AppSettings["MandrillBaseUrl"];
static Guid MandrillKey = new Guid(ConfigurationManager.AppSettings["MandrillKey"]);
public static bool SendActivationEMail(BLL.TrialSignup ts, out string errorMsg)
{
string activationLink =
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/Register/Activation.aspx?id=" + ts.Id;
//send-template(string key, string template_name, array template_content, struct message)
dynamic sendParams = new ExpandoObject();
sendParams.key = MandrillKey;
sendParams.template_name = "Secret Project Trial Activation";
sendParams.template_content = new List<dynamic>();
sendParams.message = new ExpandoObject();
sendParams.message.subject = "Here's your Secret Project activation email";
sendParams.message.from_email = "info@SecretProject.com";
sendParams.message.from_name = "Secret Project";
sendParams.message.to = new List<dynamic>();
sendParams.message.to.Add(new ExpandoObject());
sendParams.message.to[0].email = ts.EMail;
sendParams.message.to[0].name = ts.Name;
sendParams.message.track_opens = true;
//sendParams.message.track_clicks = true;
sendParams.message.global_merge_vars = new List<dynamic>();
sendParams.message.global_merge_vars.Add(new ExpandoObject());
sendParams.message.global_merge_vars[0].name = "NAME";
sendParams.message.global_merge_vars[0].content = ts.Name;
sendParams.message.global_merge_vars.Add(new ExpandoObject());
sendParams.message.global_merge_vars[1].name = "LINK";
sendParams.message.global_merge_vars[1].content = activationLink;
errorMsg = string.Empty;
MandrillError merr = SendMessage(sendParams);
switch (merr)
{
case MandrillError.OK:
return true;
case MandrillError.WebException:
case MandrillError.HttpNotOk:
errorMsg = "There was an issue sending your activation e-mail. Please try again later or call us directly.";
break;
case MandrillError.Invalid:
errorMsg = "Your email address appears to be invalid. Please try again with a valid address, or call us directly.";
break;
case MandrillError.Rejected:
errorMsg = "Your activation email was rejected. Please try again with a valid address, or call us directly.";
break;
case MandrillError.Unknown:
errorMsg = "There was an unknown problem sending your activation email. Please try again, or call us directly.";
break;
}
return false;
}
public static bool SendSalesNotification(BLL.TrialSignup ts)
{
dynamic sendParams = new ExpandoObject();
sendParams.key = MandrillKey;
sendParams.template_name = "Secret Project Trial Sales Notification";
sendParams.template_content = new List<dynamic>();
sendParams.message = new ExpandoObject();
sendParams.message.subject = "Secret Project Trial Account Notification";
sendParams.message.from_email = "info@SecretProject.com";
sendParams.message.from_name = "Secret Project";
sendParams.message.to = new List<dynamic>();
sendParams.message.to.Add(new ExpandoObject());
sendParams.message.to[0].email = ConfigurationManager.AppSettings["SalesEmail"];
sendParams.message.to[0].name = "Secret Project Sales";
//sendParams.message.track_opens = true;
//sendParams.message.track_clicks = true;
sendParams.message.global_merge_vars = new List<dynamic>();
sendParams.message.global_merge_vars.Add(new ExpandoObject());
sendParams.message.global_merge_vars[0].name = "NAME";
sendParams.message.global_merge_vars[0].content = ts.Name;
sendParams.message.global_merge_vars.Add(new ExpandoObject());
sendParams.message.global_merge_vars[1].name = "COMPANY";
sendParams.message.global_merge_vars[1].content = ts.CompanyName;
sendParams.message.global_merge_vars.Add(new ExpandoObject());
sendParams.message.global_merge_vars[2].name = "EMAIL";
sendParams.message.global_merge_vars[2].content = ts.EMail;
MandrillError merr = SendMessage(sendParams);
switch (merr)
{
case MandrillError.OK:
return true;
case MandrillError.WebException:
case MandrillError.HttpNotOk:
case MandrillError.Invalid:
case MandrillError.Rejected:
case MandrillError.Unknown:
break;
}
return false;
}
private static MandrillError SendMessage(dynamic sendParams)
{
ILog _log = log4net.LogManager.GetLogger("Mandrill/SendMessage");
string url = MandrillBaseUrl + "/messages/send-template.json";
var http = new HttpClient
{
Request = { Accept = HttpContentTypes.ApplicationJson }
};
EasyHttp.Http.HttpResponse response;
try
{
response = http.Post(url, sendParams, HttpContentTypes.ApplicationJson);
}
catch (WebException ex)
{
_log.ErrorFormat("Error: WebException - {0}", ex.Message);
return MandrillError.WebException;
}
if (response.StatusCode != HttpStatusCode.OK)
{
_log.InfoFormat("Response = {0} - {1}", response.StatusCode, response.StatusDescription);
_log.Info(response.RawText);
return MandrillError.HttpNotOk;
}
dynamic rv = response.DynamicBody;
_log.InfoFormat("email: {0}, status: {1}", rv[0].email, rv[0].status);
string send_status = rv[0].status;
if (send_status == "sent" || send_status == "queued")
return MandrillError.OK;
// otherwise, it should be "rejected" or "invalid"
if (send_status == "invalid")
{
return MandrillError.Invalid;
}
if (send_status == "rejected")
{
return MandrillError.Rejected;
}
// unexpected...
return MandrillError.Unknown;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment