Gotcha – Sending Emails with MonoRail

Update: James did not think my original post was very clear (and I agreed). I hope that this new version is easier to read and understand.

We use the technique described in MonoRail: Sending Emails to send emails from our controllers. Our emails, in some cases, have many recipients in the to, cc and bcc. The lists are formatted like the following in our datastore:

mail1@mydomain.com,mail2@mydomain.com,mail3@mydomain.com

MonoRail requires that email addresses be separated by semicolons when sending emails as described by MonoRail: Sending Emails (The RFC 2822 says email addresses should be separated by a comma). Given this requirement, we then turned our lists into the following:

mail1@mydomain.com;mail2@mydomain.com;mail3@mydomain.com

The emails were sent correctly while using MonoRail RC2.After our first release of sothebyshomes.com, we upgraded MonoRail to the latest good build on the trunk. After upgrading, sending emails would fail with the following exception message.

The specified string in not in the form required for an email address.

I found the following note on the bottom of the MailMessage Constructor documentation.

NOTE: You cannot use the semi-colon for the delimiter. You must use the commaor the constructor will throw a System.Format exception. You will see thefollowing message: " The specified string in not in the form required for an email address. "

The upgraded version of MonoRail uses the new .NET 2.0 way of sending emails (RC2 did not). System.Net.Mail is more strict than the old .NET 1.1 way. This error took me an hour or so to track down, so hopefully this post will save you some time.

Advertisements

3 Responses

  1. Wouldn’t it be nice:

    listing.Office.ContactAgentCC.Replace(‘;’).With(‘,’);

  2. I guess we will have to wait for C# 3.0. Or… we could just use Ruby.

  3. Looking back, perhaps the comma separator could have been left in, for backwards-compatibility. That said, there are other things that might break projects designed for RC2, such as changes to the way validators work, so maybe adding all the breaking changes in one go is for the best.

    Nice blog by the way. I am having a look at using dynamic actions in a project. They seem useful, although the same part of me which balks at using untyped programming languages, or those without compile-time checking, is trying to make itself heard here aswell. Hopefully the warning will take verbal form tomorrow and I’ll be able to understand it more 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: