Using the MailDefinition class in .NET Framework 4 Console Application, Visual Studio 2010

One of my colleagues pinged me to check, how to use the MailDefinition class for creating formatted mails from a .NET Console Application.  She was referring to this article http://www.search-this.com/2009/02/05/aspnet-sending-email-both-in-html-and-plain-text/ which seems to be building a nice template email that can be sent. 

But this article was related to ASP.NET and hence didn’t have issues in referencing the MailDefinition Class which is part of System.Web.UI.WebControls namespace.

The application she was building was a Console Application.  However, it made sense that these namespaces weren’t available by default since a Console Application, by no means would require a System.Web.UI (related to UI) namespace unless there is a specific need.

First thing that came to my mind was, we should refer System.Web so that the subsequent namespace could be added.  When we checked adding Reference to System.Web from the Console Application, it didn’t show up.  The closest match we could find was System.Web.Services which wouldn’t be helping us in this case.

image

Then, I realized that by default, .NET Framework 4.0 based Console Applications/Windows Applications target the .NET Client Profile (I had written about it earlier http://geekswithblogs.net/ranganh/archive/2010/05/24/visual-studio-2010-winform-application-ndash-unable-to-resolve-custom.aspx)

So, we changed the Target Framework to .NET Framework 4 (instead of the default .NET Framework 4 Client Profile) and built the solution.    After that, in Add Reference, we could locate the System.Web namespace

image

After that, we could type MailDefinition in the Console Application, as below:-

MailDefinition message = new MailDefinition();

Note that MailDefinition still needs a reference to the specific System.Web.UI.WebControls namespace which it is part of.  But the good thing in Visual Studio 2010 is that, you can right click on a known class and Resolve it to automatically reference the assembly.

image

It would automatically add the appropriate reference in the using section.  In this case, it added the System.Web.UI.WebControls namespace.  After that it was all breeze and she could proceed with the MailDefinition class. 

Cheers !!!

Print | posted on Thursday, May 12, 2011 1:15 PM

Comments on this post

# re: Using the MailDefinition class in .NET Framework 4 Console Application, Visual Studio 2010

Requesting Gravatar...
Worked like a charm. Thanks again!
Left by Patrick on Aug 02, 2012 1:33 AM

Your comment:

 (will show your gravatar)