C#: Send a Bronto Email From a .NET Website or Application

2/9/2015 4:57:16 PM

Bronto is an email marketing service which can be used to sending transactional emails. The Bronto Web Service can be used from a .NET website or application.

//add service reference to Visual Studio pointing to the Bronto wsdl.
//bronto wsdl: https://api.bronto.com/v4?wsdl
//name the reference MyBronto or whatever name you like

//your bronto api token
string token = "YOU_API_TOKEN";

//message template to send
//this can be found my opening an existing template
//in the bottom right hand corner, you will see Message API ID: ########################
string messageTemplateId = "###########################";

//email to send to
string toEmail = "user@example.com";

//name to send to
string toName = "Mr. Example";

//the from email
string fromEmail = "user@example.com";

//the from name
string fromName = "Mr. Sender";

//replacement values to add to the email
//these replacement tags must be added to your template. ex: Dear %%#first_name%%,
List<KeyValuePair<string, string>> replacementValues = new List<KeyValuePair<string, string>>();
replacementValues.Add(new KeyValuePair<string, string>("first_name", "John"));
replacementValues.Add(new KeyValuePair<string, string>("last_name", "Doe"));

//create bronto client
MyApp.MyBronto.BrontoSoapPortTypeClient brontoClient = new MyApp.MyBronto.BrontoSoapPortTypeClient();        

//get bronto session Id
string sessionId = brontoClient.login(token);

//create bronto session header
//the sessionHeader can be cached and re-used if sending multiple emails
MyApp.MyBronto.sessionHeader sessionHeader = new MyApp.MyBronto.sessionHeader();
sessionHeader.sessionId = sessionId;

//type of contact and delivery type
string contactStatus = "transactional";
string deliveryType = "transactional";

//if testing
//contactStatus = "test";
//deliveryType = "test";    
	
//create contact object(s) - only 1 contact at the moment
MyApp.MyBronto.contactObject[] contacts = new MyApp.MyBronto.contactObject[]
{ 
	new MyApp.MyBronto.contactObject()
	{
		email = toEmail,
		status = contactStatus
	}
};

//add or update the contacts to the session
//needed to get a bronto contact id, could store in local db
MyApp.MyBronto.writeResult writeResult = brontoClient.addOrUpdateContacts(sessionHeader, contacts);

string contactId = "";

if (writeResult.errors != null)
{
	//log error                        
	//return false;
}
else
{
	contactId = writeResult.results[0].id;
}


//create recipient list
MyApp.MyBronto.deliveryRecipientObject[] recipients = new MyApp.MyBronto.deliveryRecipientObject[]
{
	new MyApp.MyBronto.deliveryRecipientObject()
	{
		id = contactId,
		type = "contact",
		deliveryType = "selected"
	}
};


//add message replacement values
List<MyApp.MyBronto.messageFieldObject> fields = new List<MyApp.MyBronto.messageFieldObject>();
foreach (var keyValuePair in replacementValues)
{
	fields.Add(new MyApp.MyBronto.messageFieldObject()
	{
		type = "html",
		name = keyValuePair.Key,
		content = keyValuePair.Value
	});
}


//create delivery object
MyApp.MyBronto.deliveryObject[] deliveries = new MyApp.MyBronto.deliveryObject[]
{
	new MyApp.MyBronto.deliveryObject()
	{
		startSpecified = true,
		start = DateTime.Now.Date,
		messageId = messageTemplateId,
		type = deliveryType,
		fromEmail = fromEmail,
		fromName = fromName,
		replyEmail = fromEmail,
		authentication = false,
		replyTracking = false,
		optin = false,
		recipients = recipients,
		fields = fields.ToArray()                    
	}
};


//send email
writeResult = brontoClient.addDeliveries(sessionHeader, deliveries);

if (writeResult.errors != null && writeResult.errors.Length > 0)
{
	//log errors
	//return false;
}
else if (writeResult.results != null && writeResult.results.Length > 0)
{           
	//return true;
}