Today I encountered a rather obscure error while maintaining some old ASP.Net code (Framework 1.1). One of our mail servers is going to be replaced with another one. So naturally we needed to change configurations for those applications that send emails using the old server.
This change affected one of the applications I wrote a while ago. Since I stored all the configuration information inside the web.config file, I thought it was just a simple matter of changing the server from ‘server1′ to ‘server2′ and then I was done. Well, as it turned out, the result was totally unexpected.
Upon finishing the configuration change, I decided to test the application even though I was sure that everything would be working just fine. I was wrong, instead of getting the expected results, I got the following error message while trying to send email through the application:
Could not access ‘CDO.Message’ object.
Exception has been thrown by the target of an invocation.InnerException:
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object args, Boolean byrefModifiers, Int32 culture, String namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object args, ParameterModifier modifiers, CultureInfo culture, String namedParameters)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object args)
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Type type, Object obj, String methodName, Object args)
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object args)
Since the only thing changed was the server configuration, I thought that it was definitely a configuration problem of the new mail server. But that could not have been the problem as many applications had already been migrated over and they were working just fine.
After a Google search, it became apparent to me that many people out there are having the same issue. But no information seemed to point to a definitive answer. And the tricks mentioned in various articles did not solve my particular problem. After some struggle, I found out that the culprit was actually my MailMessage.From field. Instead of an actual email address, I had always used some random names (e.g. system notification, etc.). And apparently, the spaces in the From field string would cause the error I mentioned above. And if I remove all the spaces in the From field, the exception goes away. Since the SmtpMail object is simply a wrapper for the Win32 CDO (Collaborative Data Objects), the error was poorly propagated back (like many of the errors in .Net as I mentioned previously. See 1, 2, 3).
Indeed, had the error message been something more clear like “Error: From field must not contain any space“, I would not have spent the good portion of the afternoon hunting for bugs (I still think it must have something to do with the new mail server’s configuration, since this code has been working with other mail servers).