Fixing a nasty problem with System.Web.Mail.SmtpMail.Send

Posted: (EET/GMT+2)

 

Last week, I was debugging a Delphi for .NET application (using .NET 1.1) that reads data from a database, and sends e-mail with the System.Web.Mail.SmtpMail class depending on the data. Now, when I called the static Send method (or should I say, class method) of the SmtpMail class, I always got the following error:

System.Web.HttpException: Could not access 'CDO.Message' object.
---> System.ArgumentException: Value does not fall within the expected range.
   at System.Web.Mail.LateBoundAccessHelper.SetProp(Type type,
      Object obj, String propName, Object propValue)
   at System.Web.Mail.LateBoundAccessHelper.SetProp(Object obj,
      String propName, Object propValue)

The application has been working for years with the exact same code, and now suddenly it started to fail. I tried naturally googling (or rather, "liveing" as I use Live.com most often nowadays) for the error, but most referred to invalid registration of CDOSYS.DLL, but those tips (all nine, in fact) were not helpful in my case.

Luckily, I solved the issue. There was something strange (something I've not pinpointed yet) in the database access in Borland's BDP.NET drivers, and they returned database data with about 10 KB of null characters appended to the database data. This caused the CDO components that the SmtpMail class uses to fail with the above error message. I simply converted the null characters into spaces and then trimmed the string, and thus the problem was solved.

Note that it was difficult to find out about the null characters in the first place; Delphi's debugger stopped showing the string data up to the first null character. I only figured out about this when I started comparing String.Length to the actual contents of the string. Beware!