Many applications send email. After all it is the preferred binding for async app to human communication. I noticed that many of my colleagues were creating samples that simulate email by dropping a text file into a directory. Then they would open the file and copy and paste a GUID into a form. There is a much better way of course. In the real world people would send an HTML mail with a link that brings you back to the website with the GUID in place.
Nearly everyone has done this with a web site where you register with the site, they send you an email to confirm and then you click a link in the email to complete the registration process. I decided to build out this scenario using a workflow to manage the registration process but then I ran into the issue of how to test the email.
At first I thought I would just use Hotmail. This would work for a demonstration but you have to have network connectivity and verifying that the message arrived would require coding to open the mailbox with POP3 and look for a particular message. Then I found an easier way. You can configure the SmtpClient to drop mail messages into a folder rather than send them.
Of course, many of you will say “Duh! I can’t believe Ron didn’t know about this…”. Well it was news to me so I’m sure that some people didn’t know about it either so here is how it works.
First off, here is the very simple SendMail activity (using .NET 4 Beta 1)
Now I want to test my SendMail class. So I created a Unit Test project and added an app.config file with the necessary configuration magic.
Now all that I need to do is write the test. I want to be sure that when I send a mail that a file ends up in the c:\maildrop directory. The file will have a random GUID as the name. I’m not worried about reading the file to be sure that it contains the correct text. That would be testing the SmtpClient class which I assume works properly.
And the really cool thing is that you can open this file with Windows Live Mail and click the link if you want to demonstrate how it works.
I decided to screencast this code check it out
endpoint.tv Screencast - Spike on Workflow Managed Email Verification