Work Journal 2013-feb-05

Working from home with a baby who is still to sick to go to the nursery. Time to finish the prototype for Amazon SES, but first:

Today! From the Interwebs!

If you need cheering up - you should follow: @EmrgencyKittens (I assume you like cats). I also stumbled on a project that looks very interesting. No need to install (YAY!) - it's the portableapps.com project. I did try it out but got errors when trying to get the firefox and chrome browsers install. The idea is really good though and I will experiment a bit more with it.

Sending emails with Amazon SES

Yesterday I did some research on using Amazon SES and started on making a prototype for using the service from the project I'm working on. It is really quite easy. Here's the code to send an email:

class FeliksEmailSender
{
	private readonly SmtpClient _smtpClient;

	public FeliksEmailSender(string host, int port, 
		string smtpUserName, string smtpPassword)
	{
		_smtpClient = new SmtpClient(host, port){EnableSsl = true, 
			Credentials = new NetworkCredential(smtpUserName, 
				smtpPassword)};
	}

	public void Send(string fromEmail, string toEmails, 
		string emailSubject, string emailBody, 
			string attachmentPath )
	{
		var mail = new MailMessage(fromEmail, 
			toEmails.Replace(';', ','))
		{
			SubjectEncoding = Encoding.UTF8,
			Subject = emailSubject,
			BodyEncoding = Encoding.UTF8,
			Body = emailBody,
			Attachments = {new Attachment(attachmentPath)}
		};
		_smtpClient.Send(mail);
	}
}
The host, post, smtpUserName and smtpPassword are configuration value (found in the AWS console under SES - smtpUserName and smtpPassword must be generated the first time.). The toEmails.Replace(';', ',') is a hack around the fact that the system used to integrate with Outlook clients. Outlook separates email addresses with ';' by default - SmtpClient separates email addresses with ','. There is more to be said about SmtpClient, but that will be a blogpost in it self. Anyway, this is how an email is sent in general from .NET (except for the system specific details like an email always has exactly one attachment) and that is just really nice about Amazon SES.

Amazon SES Testing

But what about testing! I hear you say. Well, that's always the problem with system integrations. Here is how I did (with NUnit):

[Ignore("Integration test")]
[Test]
public void SendEmailWithAttachment()
{
	_feliksEmailSender.Send(_fromEmail, _toEmail, _emailSubject, 
		/*emailBody:*/ System.Reflection.MethodBase.
			GetCurrentMethod().Name, _attachmentPath);
	//Check email manually.
}

[Ignore("Integration test")]
[Test]
public void SendEmailWithAttachmentToMultipleReceivers()
{
	var toEmails = _toEmail + ";" + _anotherToEmail;
	_feliksEmailSender.Send(_fromEmail, toEmails, _emailSubject, 
		/*emailBody:*/ System.Reflection.MethodBase.
			GetCurrentMethod().Name, _attachmentPath);
	//Check emails manually.
}
Basically that means that I made sure the emails are send the same way every time and then I check the result manually.


Work Journal – a diary on what I did work/programming related today.