Error when sending Html Mail from ASP.net WebApp

On an ASP.net site (1.1), I was trying to send an email message using good old SmtpMail.Send(MailMessage message). When I did so, I got the following message “could not access CDO.message object“. After some Googling, I tried out all of the different security/smtp/relary/permissions related suggestions (most can be found on the systemwebmail.com site, a very handy reference if you have some questions regarding use of the System.Web.Mail library in ASP.net 1.1). Still not working. I checked the InnerException. It looked something like this:

InnerException: {“Unknown Error” }
Message: “Exception has been thrown by the target of an invocation.”
Source: “mscorlib”
StackTrace:
” at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)\r\n
at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)\r\n
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)\r\n
at System.Web.Mail.LateBoundAccessHelper.SetProp(Type type, Object obj, String propName, Object propValue)\r\n
at System.Web.Mail.LateBoundAccessHelper.SetProp(Object obj, String propName, Object propValue)”

But nothing apparently wrong there. The one weird thing that I could find is that if I set MailMessage.BodyFormat = MailFormat.Html, it would not work. If I set it to MailFormat.Text, it would work fine. Very suspicious.

In the end, the following solution worked for me. It was posted by Kyle Parker on the DotNetNuke forums:

  1. Close Visual Studio
  2. Open a Command Window
  3. regsvr32 cdosys.dll /u
  4. iisreset /restart
  5. regsvr32 cdosys.dll
  6. resume work in VS

After unregistering and reregistering this DLL, suddenly I was able to send Html mail again from my site. Kyle reported on his post that this only worked as a temporary solution. It is working for me so far.

Anyone know why this happened? Some other program messed up the dll that handles Html mail?

Programatic access to the ConnectionStrings section of your Web.Config file in ASP.net 2.0

Back with ASP.net 1.1, if you set a connection string in your Web.config file, you could set it with something like this:

<appSettings>
  <add key="ConnectionStringName" value="ConnectionStringData" />
</appSettings>

and get access to the string in your code with something like this:

string connString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionStringName"];

With ASP.net 2.0, while you can still use appsettings if you so desire, there are now subsections allowed in the Web.config, one of which is set aside for database connection strings. Here is how to use it:

<connectionStrings>
  <add name="ConnectionStringName" connectionString="ConnectionStringDetails"/>
</connectionStrings>

The good news with this is that the connection string within your Web.config file is no longer just some string (that while you may know it is a connection string, your application has no intrinsic knowledge of this). Your website can now access this string in a number of different ways, in general making your life easier (especially when using the Website Configuration screens, or any of the built-in data access controls).

All of this is old news. However, while the process for retrieving a connection string in 2.0 is well-documented when you are doing it from within the setup wizard of a DataAccess control, I had some trouble figuring out how to retrieve it solely within the code. Eventually (with some help from Google) I was able to find the exact syntax, and I am passing it on here so that it might help someone else facing the same problem.

string conn =  System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;

The Server Failed to Load Application

Due to some bad problems with my main machine, I have been in the process of bringing my backup laptop (Inspiron 5150, 768mb RAM, almost three years old, recently reformatted) up to speed for my development needs. This included installing Crystal Advanced Developer 10 (required for one of the ASP.net webapps that I am doing maintenance on).

So I installed it, as I have done on a number of computers in the past. I then went and tried to run browse the webapp in question (using http://localhost/appName) and received an HTTP 500 error (IE7 politely told me that this indicated a server error, and that the website might be under maintenance).

I tried loading a totally different webapp that had nothing to do with Crystal. Same error. Reboot, same error. Not good.

So I looked in the System Event Viewer, and saw a bunch of the following warnings:

The server failed to load application ‘/LM/W3SVC/1/Root/appName’. The error was ‘General access denied error’

OK. What is that supposed to mean? And why was it killing all of my webapps?

After a little bit of Googling, I found a page by Brett Hill in IIS Insider (March 2005), with one of the subsections titled: Resolving the Error: The server failed to load application…

The most likely cause of this problem is that the DTC coordinator service has not started. If it has started, then look in the Event Viewer for a message regarding a failed logon for the IWAM_servername account.

So I went into Services, and the Distributed Transaction Coordinator service was running. I stopped the service and restarted it, and voila, all of my sites would run again. Hopefully this situation will continue after my next reboot. If it does, the above article has some more advice on ways around this.

I have installed this program a number of times already on other machines without experiencing this error. Anyone know what may have caused it.

Items of Interest: 2006.04.03

Technorati Tags: , , , , , , , , , , ,

Custom Paging in ASP.net 2.0

Check out Scott Mitchell’s treatment of the subject on the 4Guys website:

As someone who has had to create a long stored procedure to handle custom paging in ASP.net 1.1 (create temporary table, generate query to load IDs into the table, query the temp table with some variables to pull out the records that you want), I am especially excited about the Row_Number() keyword in SQL Server 2005, and the savings in development time (debugging & headaches) that will come along with its proper use.

ASP.net 2.0 Looking Pretty Slick

First a disclaimer: I have never used Visual Studio 2005 (except for an early beta Express edition) nor have I used ASP.net 2.0 (I plan to start in the next couple of days). I have been doing lots of ASP.net programming and application development using the .Net Framework (1.14322), C#, VS 2003 and SQL Server 2000.

That said, I am getting personally excited (in a professional way) about the improved development potential with ASP.net 2.0

A few things that I have seen recently that have influenced my opinion enough to write the previous sentence:

  • Scott Guthrie has posted an account of how MySpace is using ASP.netand how switching to the new platform (fresumably from ColdFusion) affected their performance:
    • They are processiong 1.5 billion page views per day, reaching 2.3 concurrent users
    • After switching to ASP.net 2.0, their server utilization went down from 85% to 27%!

    So if you are considering the platform, you wont have to be concerned about any scaling or performance issues (assuming you know what you are doing). (I wonder how php would compare, with caching and everything else. Would it even be 50% as efficient as pre-compiled code? 30%?)

  • The ATLAS framework has just received a go-live license with its March CTP release. ATLAS is Microsoft’s version of Ajax for ASP.net. It may not be ROR, but if you check out this demonstration video (worth watching for both the ATLAS demo as well as the demonstration of the new improvements to the product that make development go faster) I hope you will agree that it is definitely something worth trying out.
  • For just a quick taste of other features touched by the upgrade (as well as totally new additions), check out some of the webcasts on the ASP.net Developer Center

They may not ship everything on time…but once they get their products out the door, they (often?!) can really shine.