﻿<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>MidnightCoder.net</title>
    <description>description</description>
    <link>http://www.midnightcoder.net/</link>
    <generator>Argotic Syndication Framework, http://www.codeplex.com/argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <item>
      <title>Pop-up Previews with Prototype</title>
      <description>&lt;p&gt;My friend Chris recently mentioned that he was looking for a way to create pop-up previews. For example,&amp;nbsp;displaying an image of a product when you hover over the item in a shopping cart. It's a solved problem with things like &lt;a href="http://www.nickstakenburg.com"&gt;Nick Stackenburg&lt;/a&gt;'s &lt;a href="http://www.nickstakenburg.com/projects/prototip/"&gt;ProtoTip&lt;/a&gt;, but as I've never built something like that before myself, I thought I'd give it a go.&amp;nbsp;Armed with &lt;a href="http://www.prototypejs.org"&gt;prototype&lt;/a&gt;, here's what I came up with.&lt;/p&gt;
&lt;p&gt;I wanted&amp;nbsp;it to be as simple as possible, preferably a one-liner like this:&lt;/p&gt;
&lt;pre class="jscript" name="code"&gt;hookUpPreviewTo( 'example1' );&lt;/pre&gt;
&lt;p&gt;Where example1 is the id of an element on the page, and there is another element with an id of example1_preview. Convention over configuration ;)&lt;/p&gt;
&lt;p&gt;There are basically two events you need to handle in order to show and hide the preview: onmouseover, and onmouseout.&lt;/p&gt;
&lt;pre class="jscript" name="code"&gt;hookUpPreviewTo = function( item ) {
  repositionPreviewFor( item );
  $( item ).observe( 'mouseover', showPreview );
  $( item ).observe( 'mouseout', hidePreview );
}&lt;/pre&gt;
&lt;p&gt;Ignoring line 2 for a moment, line 3 and 4 are prototype magic. $(item) grabs a reference to an element on the page, much the same as document.getElementById( item ), and&amp;nbsp;observe assigns an event handler to the specified event.&lt;/p&gt;
&lt;p&gt;repositionPreviewFor( item ) moves the preview for item to just underneath it on the page. It looks like this:&lt;/p&gt;
&lt;pre class="jscript" name="code"&gt;repositionPreviewFor = function( item ) {
  var elementId = $(item).id;
  var elementPreviewId = elementId + '_preview';

  $(elementPreviewId).clonePosition( elementId,
    { setHeight: false, setWidth: false, offsetTop: 20 } );
}&lt;/pre&gt;
&lt;p&gt;Lines 2 and 3 get the id of the item to show the preview for, and generates the id for the matching preview element. Line 5-6 is more prototype magic. This time, it copies the position from the item element into its preview and offsetTop: 20 then moves the preview down a bit. 20 is one of those dreaded magic numbers, and really should be calculated. It'll do for a quick demo :)&lt;/p&gt;
&lt;p&gt;hidePreview and showPreview are pretty basic and do precisely what you would expect:&lt;/p&gt;
&lt;pre class="jscript" name="code"&gt;showPreview = function( event ) {
  var elementPreviewId = Event.element(event).id + '_preview';

  $(elementPreviewId).show();
}&lt;/pre&gt;
&lt;p&gt;hidePreview is the same, it just calls .hide() instead.&lt;/p&gt;
&lt;p&gt;For this to work, you need two things in your html: an element with a known id and&amp;nbsp;another element with the same id with &amp;quot;_preview&amp;quot; tacked on the end. In order to hook up the event handlers, we can use a little more prototype magic, to attach them once the page has finished loading:&lt;/p&gt;
&lt;pre class="jscript" name="code"&gt;Event.observe( window, 'load', function() {
  hookUpPreviewTo( 'example1' );
  hookUpPreviewTo( 'example2' );
  hookUpPreviewTo( 'example3' );
});&lt;/pre&gt;
&lt;p&gt;Now when you hover over the element with the id of examle1, the element with the id of example1_preview will be displayed below it. When you move your mouse away from example1, example1_preview will be hidden again.&lt;/p&gt;
&lt;p&gt;&lt;img height="604" alt="Hover Preview demo screenshot" width="486" src="/UserFiles/Image/HoverPreviewDemo.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;If you'd like to try it yourself, and have a look at the code, you can &lt;a href="/UserFiles/File/HoverDemo.zip"&gt;download it here&lt;/a&gt;.&lt;/p&gt;</description>
      <link>http://www.midnightcoder.net/blog/viewpost.rails?postId=49</link>
      <author>Tim Wilde</author>
      <comments>http://www.midnightcoder.net/blog/viewpost.rails?postId=49#comments</comments>
      <pubDate>Tue, 18 Mar 2008 15:17:00 +0000</pubDate>
      <guid isPermaLink="true">http://www.midnightcoder.net/blog/viewpost.rails?postId=49</guid>
    </item>
    <item>
      <title>Big Refactorings!</title>
      <description>&lt;p&gt;I decided it was time to catch up with some of the to-do list items I've made for myself for this blog. On that list are some immediately useful things like integrating Alex Gorbatchev's &lt;a href="http://code.google.com/p/syntaxhighlighter/"&gt;SyntaxHighlighter&lt;/a&gt;, and &lt;em&gt;finally&lt;/em&gt; getting around to implementing publsihing control so I can publish posts when they're finished, and leave them hanging around when they're not.&lt;/p&gt;
&lt;p&gt;Behind the scenes is where the really big changes are going to happen though, as I want to use Windsor with MonoRail, which I've been having a lot of success with on other projects. This means I'm going to be changing just about every single class, removing &amp;quot;poor man's dependency injection&amp;quot; in the form of constructors like this:&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;public BlogPostRepository(): this( new BlogPostDao() ) {}&lt;/pre&gt;
&lt;p&gt;I also want to add notification of new comments, as I often only notice them when I read the blog myself; which isn't often, as I subscribe to my own RSS! This will also help me react more quickly to comment spam, too (of which I've had a whopping three so far!).&lt;/p&gt;
&lt;p&gt;Ok. Enough talk; more code!&lt;/p&gt;</description>
      <link>http://www.midnightcoder.net/blog/viewpost.rails?postId=48</link>
      <author>Tim Wilde</author>
      <comments>http://www.midnightcoder.net/blog/viewpost.rails?postId=48#comments</comments>
      <pubDate>Sat, 15 Mar 2008 17:51:13 +0000</pubDate>
      <guid isPermaLink="true">http://www.midnightcoder.net/blog/viewpost.rails?postId=48</guid>
    </item>
    <item>
      <title>Critical Code Mass</title>
      <description>&lt;p&gt;Sounds a bit ominous, doesn't it? Well, I actually mean it in a good way: the point at which you don't have to think up more code for a particular feature for a new project; rather, you can check something out of your code repository and get on with working on the problem at hand.&lt;/p&gt;
&lt;p&gt;That kind of &amp;quot;critical&amp;quot; code mass, I'm very happy with, and it's where I'm beginning to find myself more and more recently. For example, when a new project requires authentication (and don't they all?!) I can just check out the code I wrote when writing the MembershipProvider when I wrote the corporate Intranet.&lt;/p&gt;
&lt;p&gt;We recently had a company come in to install a centralised printing system for us, and they needed a way to identify the owner of a print job, and also a way to associate their data with that user. After a quick dive into my code repository, I had the answer: the membership provider code for user identification, and code from where I integrated our time-and-attendance system with the Intranet. An hour later we had a tool which we used to associate network user accounts, door-entry swipe card numbers, and their system identification tokens which produced data ready for their system to use.&lt;/p&gt;
&lt;p&gt;Because the majority of my code is now driven out with TDD, I can also easily pull small parts in isolation, along with their tests, and quickly reuse them to solve other problems. It's a very nice position to be in!&lt;/p&gt;
&lt;p&gt;See? Critical code mass, but in a good way :)&lt;/p&gt;</description>
      <link>http://www.midnightcoder.net/blog/viewpost.rails?postId=47</link>
      <author>Tim Wilde</author>
      <comments>http://www.midnightcoder.net/blog/viewpost.rails?postId=47#comments</comments>
      <pubDate>Sat, 15 Mar 2008 15:58:13 +0000</pubDate>
      <guid isPermaLink="true">http://www.midnightcoder.net/blog/viewpost.rails?postId=47</guid>
    </item>
    <item>
      <title>Enum Selects in MonoRail templates</title>
      <description>&lt;p&gt;This is one of those &amp;quot;blogged for my own reference&amp;quot; posts, but it's a neat trick.&lt;/p&gt;
&lt;p&gt;In MonoRail you use various helpers to build your template. There's one for building forms, which makes it much easier to work with objects to build your html form.&lt;/p&gt;
&lt;p&gt;One of the things I've just been playing with is creating a HTML Select element from an Enum. In my case, I need an enum called Priority to be a drop-down list of options.&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;public enum Priority { Urgent, High, Medium, Low, Hold }&lt;/pre&gt;
&lt;p&gt;Which I would like to appear like this, with Medium selected by default:&lt;/p&gt;
&lt;p&gt;&lt;select&gt;
&lt;option value="0"&gt;Urgent&lt;/option&gt;
&lt;option value="1"&gt;High&lt;/option&gt;
&lt;option value="2" selected="selected"&gt;Medium&lt;/option&gt;
&lt;option value="3"&gt;Low&lt;/option&gt;
&lt;option value="4"&gt;Hold&lt;/option&gt;
&lt;/select&gt;&lt;/p&gt;
&lt;p&gt;The MonoRail template (using Brail syntax) is as follows:&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;${ Form.Select( 'project.Priority', Priority.Medium, Form.EnumToPairs( typeof( Priority ) ), { @value: @first, @text: @second } ) }&lt;/pre&gt;
&lt;p&gt;Breaking that down, the first parameter to Form.Select is the name for the html form field; the id will be generated with the dot replaced with an underline &amp;quot;project_Priority&amp;quot;.&lt;/p&gt;
&lt;p&gt;The second parameter Priority.Medium, specifies the value to select in the list.&lt;/p&gt;
&lt;p&gt;Next is the really useful part, Form.EnumToPairs, which generates the IEnumerable data from an Enum type. It creates an array of Pair&amp;lt;int, string&amp;gt;. The int field is called &amp;quot;first&amp;quot;, and the string field is called &amp;quot;second&amp;quot;, which we'll need next.&lt;/p&gt;
&lt;p&gt;The last parameter of the Form.Select is a dictionary specifying options. In the example above, this specifies the property names to use for the value and text for the option elements of the select. The format &amp;quot;@value: @first&amp;quot; defines a dictionary entry with a key of &amp;quot;value&amp;quot;, and a value of &amp;quot;first&amp;quot;.&lt;/p&gt;</description>
      <link>http://www.midnightcoder.net/blog/viewpost.rails?postId=45</link>
      <author>Tim Wilde</author>
      <comments>http://www.midnightcoder.net/blog/viewpost.rails?postId=45#comments</comments>
      <pubDate>Tue, 04 Mar 2008 12:38:18 +0000</pubDate>
      <guid isPermaLink="true">http://www.midnightcoder.net/blog/viewpost.rails?postId=45</guid>
    </item>
    <item>
      <title>Music to code to...</title>
      <description>&lt;p&gt;I'm just enjoying &lt;a href="http://www.torley.com"&gt;Torley Wong&lt;/a&gt;'s &lt;a href="http://www.archive.org/search.php?query=torley%20wong%20AND%20mediatype%3Aaudio%20AND%20collection%3Aopensource_audio"&gt;music&lt;/a&gt;, especially the track &lt;a href="http://www.archive.org/download/torley_wong-wong_songs__1b_/in_the_garden_of_hopes_dreams_vbr.mp3"&gt;In the Garden of Hopes and Dreams&lt;/a&gt;. I thought I'd share :)&lt;/p&gt;</description>
      <link>http://www.midnightcoder.net/blog/viewpost.rails?postId=44</link>
      <author>Tim Wilde</author>
      <comments>http://www.midnightcoder.net/blog/viewpost.rails?postId=44#comments</comments>
      <pubDate>Sun, 02 Mar 2008 22:03:30 +0000</pubDate>
      <guid isPermaLink="true">http://www.midnightcoder.net/blog/viewpost.rails?postId=44</guid>
    </item>
    <item>
      <title>Rails Shortcuts</title>
      <description>&lt;p&gt;I've spent the weekend writing a Ruby on Rails site in &lt;a href="http://macromates.com/"&gt;TextMate&lt;/a&gt;, and I've &lt;em&gt;only just&lt;/em&gt; found out that I can use Command+Shift+R to run the unit test the cursor is in, and Control+Shift+R to run rake tasks.&lt;/p&gt;
&lt;p&gt;Better late than never, I suppose.&lt;/p&gt;
&lt;p&gt;I've made a note to have a look at &lt;a href="http://ph7spot.com/articles/getting_started_with_autotest"&gt;Autotest&lt;/a&gt; next weekend. See if that can save me some time.&lt;/p&gt;
&lt;p&gt;I've got to say though, I'm enjoying the new migration and fixture syntax! :)&lt;/p&gt;
&lt;p&gt;But for now, it's back to .net for the working week... :/&lt;/p&gt;</description>
      <link>http://www.midnightcoder.net/blog/viewpost.rails?postId=42</link>
      <author>Tim Wilde</author>
      <comments>http://www.midnightcoder.net/blog/viewpost.rails?postId=42#comments</comments>
      <pubDate>Mon, 25 Feb 2008 01:30:06 +0000</pubDate>
      <guid isPermaLink="true">http://www.midnightcoder.net/blog/viewpost.rails?postId=42</guid>
    </item>
    <item>
      <title>HTML Tool Suggestions</title>
      <description>&lt;p&gt;I've been asked to give a training course on the basics of HTML to a couple of colleagues for which I'm just compiling a list of essential tools and plugins which will make their HTML-editing lives much easier:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://www.getfirefox.com"&gt;Firefox&lt;/a&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href="https://addons.mozilla.org/en-US/firefox/addon/1843"&gt;FireBug&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="http://developer.yahoo.com/yslow/"&gt;YSlow for Firebug&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="http://users.skynet.be/mgueury/mozilla/download.html"&gt;HTML Validator&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.flos-freeware.ch/notepad2.html"&gt;Notepad2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The idea is not to overwhelm them with tools, so the list is kept to a minimum.&lt;/p&gt;
&lt;p&gt;What would you put on your list of &lt;em&gt;essential&lt;/em&gt; HTML tools?&lt;/p&gt;</description>
      <link>http://www.midnightcoder.net/blog/viewpost.rails?postId=41</link>
      <author>Tim Wilde</author>
      <comments>http://www.midnightcoder.net/blog/viewpost.rails?postId=41#comments</comments>
      <pubDate>Fri, 25 Jan 2008 12:38:55 +0000</pubDate>
      <guid isPermaLink="true">http://www.midnightcoder.net/blog/viewpost.rails?postId=41</guid>
    </item>
    <item>
      <title>Fluent Interfaces</title>
      <description>&lt;p&gt;&lt;a href="http://www.ayende.com/blog"&gt;Oren&lt;/a&gt; recently &lt;a href="http://ayende.com/Blog/archive/2007/12/01/9889.aspx"&gt;posted an article&lt;/a&gt; comparing a Domain Specific Language (DSL) and a Fluent Interface.&lt;/p&gt;
&lt;p&gt;I'm still not entirely sure I understand the differences between a DSL and a fluent interface, but I didn't think that Oren really pushed the fluent interface example quite as far as he could have, so I thought I'd play with the idea and see what I could come up with.&lt;/p&gt;
&lt;p&gt;For comparison, here's Oren's example:&lt;/p&gt;
&lt;pre class="csharp" name="code"&gt;new FluentTask(&amp;quot;warn if website is not alive&amp;quot;)
   .Every( TimeSpan.FromMinutes(3) )
   .StartingFrom( DateTime.Now )
   .When(delegate
         {
            return WebSite(&amp;quot;http://example.org&amp;quot;)
               .IsAlive == false;
         })
   .Execute(delegate
            {
               Notify(&amp;ldquo;admin@example.org&amp;rdquo;, &amp;ldquo;server down!&amp;rdquo;);
            });&lt;/pre&gt;
&lt;p&gt;Here's what I came up with in about 30-45 minutes:&lt;/p&gt;
&lt;pre class="csharp" name="code"&gt;ITask SiteCheckTask =
   Schedule.Task( &amp;quot;Report website down-time&amp;quot; ).
      Repeat.Every( 3 ).Minutes.
      Starting( DateTime.Now ).
      If( Web.Site( &amp;quot;http://www.example.com&amp;quot; )
             .IsNotResponding() ).
      Notify( &amp;quot;admin@example.com&amp;quot;, &amp;quot;Site down!&amp;quot; );&lt;/pre&gt;
&lt;p&gt;Although it reads quite nicely from the user's perspective, behind the scenes there are eight classes and six interfaces, which makes this small demo quite complex.&lt;/p&gt;
&lt;p&gt;This works because most of the methods and properties are factory methods. For example, Schedule.Task( &amp;quot;Report website down-time&amp;quot; ) is a static&amp;nbsp;method which returns an instance of a&amp;nbsp;TaskDescriptor class:&lt;/p&gt;
&lt;pre class="csharp" name="code"&gt;public class Schedule
{
   public static ITaskDescriptor Task( string description )
   {
      return new TaskDescriptor( description );
   }
}&lt;/pre&gt;
&lt;p&gt;The Repeat property of the TaskDescriptor then does the same again: it creates an instance of a ScheduleDescriptor (passing itself in) and returns that. The ScheduleDescriptor describes the start and interval for the scheduled task.&lt;/p&gt;
&lt;pre class="csharp" name="code"&gt;public IScheduleDescriptor Repeat
{
   get { return new ScheduleDescriptor( this ); }
}&lt;/pre&gt;
&lt;p&gt;And so on. The ScheduleDescriptor's Every() method sets an internal interval field and returns itself to the caller ( return this; ). The Starting method of the ScheduleDescriptor records the DateTime, and then sets itself as the ScheduleDetails on the TaskDescriptor, which it then returns.&lt;/p&gt;
&lt;pre class="csharp" name="code"&gt;public ITaskDescriptor Starting( DateTime start )
{
   startTime = start;
   descriptor.ScheduleDetails = this;
   return descriptor;
}&lt;/pre&gt;
&lt;p&gt;The If() method on the TaskDescriptor does much the same as the Repeat property did, but it creates an ActionDescriptor this time. It accepts a StatusCheckDelegate as a parameter, which is built with the Web static class' Site() method. This returns an instance of a WebStateCheckDescriptor (beginning to see a pattern here?) which takes a string for the URL, and the IsNotResponding() method returns a delegate which (nearly) checks whether the specified URL is accessible.&lt;/p&gt;
&lt;p&gt;Phew. Plenty of indirection there!! But the question is: does it add &lt;em&gt;too much&lt;/em&gt; complexity? I think that is probably one of those questions which has to be answered on a per-project basis.&lt;/p&gt;
&lt;p&gt;You can have a trawl through &lt;a href="/UserFiles/File/CodeDrop/SchedulerDSL.zip"&gt;the code&lt;/a&gt; to see what I've done. It's certainly not anywhere near production (it doesn't even &lt;em&gt;do&lt;/em&gt; anything) but hopefully it might be useful &lt;img alt="" src="/Assets/Images/Smileys/smile.gif" /&gt;&lt;/p&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;p&gt;Anders Nor&amp;aring;s' &amp;quot;&lt;a href="http://andersnoras.com/blogs/anoras/archive/2007/07/09/behind-the-scenes-of-the-planning-dsl.aspx"&gt;Behind the scenes of the planning DSL&lt;/a&gt;&amp;quot; was the inspiration for this spike.&lt;/p&gt;</description>
      <link>http://www.midnightcoder.net/blog/viewpost.rails?postId=38</link>
      <author>Tim Wilde</author>
      <comments>http://www.midnightcoder.net/blog/viewpost.rails?postId=38#comments</comments>
      <pubDate>Mon, 03 Dec 2007 20:58:46 +0000</pubDate>
      <guid isPermaLink="true">http://www.midnightcoder.net/blog/viewpost.rails?postId=38</guid>
    </item>
    <item>
      <title>Assembly Information Problems</title>
      <description>&lt;p&gt;I decided to upgrade the OS on my desktop PC to Vista x64, yesterday. I've tried this before, but I tripped on a problem so fundamental to me, that it actually resulted in me rejecting Vista and reinstalling Windows XP.&lt;/p&gt;
&lt;p&gt;When I install Visual Studio on Vista x64 Ultimate and create a new project of any type, the Assembly Information always lists &amp;quot;Microsoft&amp;quot; for the Company field, and &amp;quot;Copyright &amp;copy; Microsoft 2007&amp;quot; in the Copyright field.&lt;/p&gt;
&lt;p&gt;This may sound like a pretty simple thing. Just change it, and get on with writing code! The problem is, this happens for every assembly I create, every time, so I would have to remember to make the change every time -- very annoying!&lt;/p&gt;
&lt;p&gt;The problem is that each project template has placeholders for this information, which is swapped out when you create a new project from them. The placeholder in this case is &amp;quot;$registeredorganization$&amp;quot;, and this is (usually) replaced with the contents of the &amp;quot;RegisteredOrganization&amp;quot; registry key in HKLM\Software\Microsoft\Windows NT\CurrentVersion, which contains the name you entered when installing Windows.&lt;/p&gt;
&lt;p&gt;As Vista doesn't actually ask you for a company name during setup, this key is empty by default, so I typed &amp;quot;MidnightCoder.net&amp;quot; into it, restarted Visual Studio, and created a new Class Library project. It was still showing &amp;quot;Microsoft&amp;quot; in the company and copyright assembly information fields.&lt;/p&gt;
&lt;p&gt;I know this would have worked on 32bit Vista, as I did precisely this on my work laptop, so this seems to be something perculiar to Vista x64.&lt;/p&gt;
&lt;p&gt;Fortunately, Microsoft, or more specifically SysInternals, gives us a tool we can use to hunt down the problem: &lt;a href="http://www.microsoft.com/technet/sysinternals/SystemInformation/processmonitor.mspx"&gt;Process Monitor&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I downloaded Process Monitor and ran it up. It instantly began filling up at a rate of knots: too much data!! The first thing I needed to do was to filter out everything but Visual Studio, so I pressed the filter shortcut (CTRL + L) and added the following:&lt;/p&gt;
&lt;p&gt;Column: Process Name&lt;br /&gt;
Relation: is&lt;br /&gt;
Value: devenv.exe&lt;br /&gt;
Action: Include&lt;/p&gt;
&lt;p&gt;This narrowed it down to Visual Studio details, so I created another Class Library and looked at Process Explorer. Amazingly, it still had too much for me to sift through, so I needed to refine my filter some more. I added this:&lt;/p&gt;
&lt;p&gt;Column: Path&lt;br /&gt;
Relation: contains&lt;br /&gt;
Value: RegisteredOrganization&lt;br /&gt;
Action: Include&lt;/p&gt;
&lt;p&gt;This time when I created (another) Class Library, I got exactly what I needed: the path Visual Studio x64 was loading the details to replace $registeredorganization$ from is:&lt;/p&gt;
&lt;p&gt;HKLM\Software\&lt;strong&gt;Wow6432Node&lt;/strong&gt;\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization&lt;/p&gt;
&lt;p&gt;Bingo. That still read &amp;quot;Microsoft&amp;quot;. I changed it to &amp;quot;MidnightCoder.net&amp;quot; and created one more Class Library, and my problem was solved, thanks to Process Explorer and a simple filter.&lt;/p&gt;</description>
      <link>http://www.midnightcoder.net/blog/viewpost.rails?postId=37</link>
      <author>Tim Wilde</author>
      <comments>http://www.midnightcoder.net/blog/viewpost.rails?postId=37#comments</comments>
      <pubDate>Wed, 28 Nov 2007 00:38:49 +0000</pubDate>
      <guid isPermaLink="true">http://www.midnightcoder.net/blog/viewpost.rails?postId=37</guid>
    </item>
    <item>
      <title>Html Email</title>
      <description>&lt;p&gt;I mentioned earlier that I'm writing some code to send an email. The emails will be the invitations to join the family portal project I also mentioned.&lt;/p&gt;
&lt;p&gt;One of the things I wanted to do is to make the emails a bit more interesting than just a paragraph of text and a link, so I've been looking at ways to send a HTML email.&lt;/p&gt;
&lt;p&gt;I found two solutions. I'm using &lt;a href="http://www.castleproject.org/monorail/index.html"&gt;Castle MonoRail&lt;/a&gt; which has can use its view templates to render and send an email, and the .net framework (2.0 and later) also supports alternate views in the MailMessage so you can add a HTML version.&lt;/p&gt;
&lt;p&gt;After some research, I decided that I wanted to embed the image into the email as externally linked images, that is, email with links back to web servers for the images, are more likely to be blocked by spam filters as it is possible to build an image URL to send information back to the web server.&lt;/p&gt;
&lt;p&gt;This ruled out the MonoRail mail aproach as it doesn't embed the images, as far as I could tell by spelunking through the code. The .net framework MailMessage, however, does support embedded images and it's pretty easy, too.&lt;/p&gt;
&lt;p&gt;To embed an image, you first have to create a HTML version of the mail:&lt;/p&gt;
&lt;pre class="csharp" name="code"&gt;AlternateView htmlView =
   AlternateView
      .CreateAlternateViewFromString( &amp;quot;&amp;lt;h1&amp;gt;Not Favourite!&amp;lt;/h1&amp;gt;&amp;lt;img src=\&amp;quot;cid:notfavourite\&amp;quot; /&amp;gt;&amp;quot;,
         Encoding.Default, &amp;quot;text/html&amp;quot; );&lt;/pre&gt;
&lt;p&gt;If you look at the image tag in that html, you'll notice the source is cid:notfavourite. The &amp;quot;cid:&amp;quot; part tells email clients to use the&amp;nbsp;linked resource called &amp;quot;notfavourite&amp;quot;. Next we need to create that&amp;nbsp;linked resource.&lt;/p&gt;
&lt;pre class="csharp" name="code"&gt;LinkedResource notFavouriteImage =
   new LinkedResource( 
      &amp;quot;images\notFavourite.jpg&amp;quot;,
      &amp;quot;image/jpeg&amp;quot; );
notFavouriteImage.ContentId = &amp;quot;notfavourite&amp;quot;;&lt;/pre&gt;
&lt;p&gt;This loads the image, sets the MIME type, and assigns a content id to the image. It's important to use the correct MIME type, as if it is wrong, the embedded image won't display properly. The ContentId also needs to match whatever is used in the image src property, &amp;quot;notfavourite&amp;quot; in this case.&lt;/p&gt;
&lt;p&gt;Next, we add the linked resource to the alternate view...&lt;/p&gt;
&lt;pre class="csharp" name="code"&gt;htmlView.LinkedResources.Add( notFavouriteImage );&lt;/pre&gt;
&lt;p&gt;... and add the alternate view to the MailMessage.&lt;/p&gt;
&lt;pre class="csharp" name="code"&gt;message.AlternateViews.Add( htmlView );&lt;/pre&gt;
&lt;p&gt;That's it. Next you just send the message as usual:&lt;/p&gt;
&lt;pre class="csharp" name="code"&gt;new SmtpClient().Send( message );&lt;/pre&gt;
&lt;p&gt;This takes its configuration from the web/app.config file, in the configuration/system.net/mailSettings/smtp section. Here's the one I used for my testing. You would need to change the details for your own environment, of course.&lt;/p&gt;
&lt;pre class="xml" name="code"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; ?&amp;gt;
&amp;lt;configuration&amp;gt;
   &amp;lt;system.net&amp;gt;
      &amp;lt;mailSettings&amp;gt;
         &amp;lt;smtp from=&amp;quot;joe.bloggs@example.com&amp;quot;
               deliveryMethod=&amp;quot;Network&amp;quot;&amp;gt;
            &amp;lt;network host=&amp;quot;127.0.0.1&amp;quot;
                     port=&amp;quot;25&amp;quot;
                     userName=&amp;quot;&amp;quot;
                     password=&amp;quot;&amp;quot; /&amp;gt;
         &amp;lt;/smtp&amp;gt;
      &amp;lt;/mailSettings&amp;gt;
   &amp;lt;/system.net&amp;gt;
&amp;lt;/configuration&amp;gt;&lt;/pre&gt;
&lt;p&gt;I've packaged up my &lt;a href="/UserFiles/File/HtmlEmail.zip"&gt;quick test project&lt;/a&gt;, so you can get started quickly. You will need to edit the app.config before you run the test. It uses NUnit as this means I don't need to mess about with a win- or web-forms app just to test a small snippet of code.&lt;/p&gt;</description>
      <link>http://www.midnightcoder.net/blog/viewpost.rails?postId=31</link>
      <author>Tim Wilde</author>
      <comments>http://www.midnightcoder.net/blog/viewpost.rails?postId=31#comments</comments>
      <pubDate>Thu, 18 Oct 2007 14:21:59 +0000</pubDate>
      <guid isPermaLink="true">http://www.midnightcoder.net/blog/viewpost.rails?postId=31</guid>
    </item>
  </channel>
</rss>