Random thoughts about everything and nothing

Archive for the ‘Article’ Category


leave a comment »

While fooling around with my Web.config I stumbled on a nice feature in ASP.NET (2.0): ExpressionBuilder classes.

Basically, these classes let you set control properties through *ahem* expressions.

When the page is parsed, the declarative expressions will be used to retrieve the actual value(s). This done through the ExpressionBuilder class.

The framework ships with three expression builder classes, each accessing different resource settings:

  • AppSettingsExpressionBuilder, allows access to the connection strings stored in section of the configuration file;
  • ConnectionStringsExpressionBuilder, allows access to the application settings stored in section of the configuration file;
  • ResourceExpressionBuilder, allows access to localized resources, typically stored in .resx files located in the App_GlobalResources and App_LocalResources folders.

To actually set these expressions, we don’t need to write it out in the property, but we can use Property Inspector > Data > Expressions.

Now the really nice part is yet to come, we can create our own ExpressionBuilders (both design/run-time support). A good place to start is here and ofcourse your local MSDN documentation.


Written by Michael

March 5, 2006 at 10:51 pm

Posted in .NET, Article

Sending mail in 2.0 – Part 1

leave a comment »

The .NET framework 2.0 has been loaded with interesting new features and classes. Beside these completely new items, some pre-existing classes have been buffed and moved to a new namespace. A good example is the System.Web.Mail, used to construct and send SMTP Messages.

Welcome to System.Net.Mail

Before proceeding, let’s take a look at the internal plumping before digging into the new features. Unlike the framework 1.X version, the System.Net.Mail libraries have been built up without relying on any interop interaction…exit CDO/CDO.SYS (Collaboration Data Objects for Windows 2000) we might say. From now on, you simple need the .NET framework 2.0 installed.

Let’s start with a very familiar piece of code for sending a message.

MailMessage mail = new MailMessage();

mail.From = new
MailAddress(“mike@hatmail.com”, “Mike Doe”);


mail.Subject = “Interesting…”;
mail.Body = “Sending mail in 2.0”;

SmtpClient smtp = new SmtpClient(“”);


Notice the basic outline of sending a message hasn’t changed. The MailMessage class is the still the foundation. It deals with creating and managing the email message. All other classes will somehow interact with this class. You will observe the first big change with the MailAddress class, used for creating email addresses.

Since the To, Cc and Bcc properties are collection we can add multiple addresses by simple calling the .Add() method. No more need for the “;” separated string when assigning multiple recipients.

Pretty similar, I can use the Attachments property to include one or more attachments to my message. These can be created from streams or file paths. Have a look at this snippet:

MailMessage mail = new MailMessage();

mail.From = new
mail.Subject = “Attack of the attachments”;
mail.Body = “Do you see

mail.Attachments.Add(new Attachment(@”c:\proj\doc1.txt”));
mail.Attachments.Add(new Attachment(@”c:\proj\doc2.txt”));

As mentioned, next to the files stored on disk you can use streams too, more on that next time…

Return to sender

One thing you would like to know when sending emails: who didn’t receive my mail. This will be handled through the SmtpFailedRecipientException and SmtpFailedRecipientsException.

Here’s the source to demonstrate:

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.From = new
mail.Subject = “Urgent”;
mail.Body = “Hope you get this message!”;
SmtpClient smtp = new

catch (System.Net.Mail.SmtpFailedRecipientsException exRecipients)

catch (System.Net.Mail.SmtpFailedRecipientException exRecipient)


My server will only allow relay mails to the “hatmail.com” domain, as such the first two addresses will cause an exception.

When thrown, I can now easily identity the faulty addresses by iterating the InnerExceptions property. Each item containing the FailedRecipient address. How’s that for simplicity!


I won’t be far off be saying that a lot of today’s applications use some sort of email (notification) .

The System.Net.Mail namespace delivers an upgraded platform for sending Smtp messages with some interesting new features. Even I you don’t need to more advanced classes in this namespace, you will still enjoy the ease of managing your mail messages. Although the explicit dependency on the CDO.sys has been removed, it is still dependent on Enterprise services (COM+).

Written by Michael

March 2, 2006 at 9:15 pm

Posted in .NET, Article