ASP.Net, CDO, and ‘The transport failed to connect to the server.’

A Web application (written in ASP.Net 1.1) that has run with no problems for months suddenly started giving a problem. The problem occurred when running the following code:

SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["SmtpServer"];

MailMessage msg = new MailMessage();
msg.BodyFormat = MailFormat.Text;
msg.From = ConfigurationSettings.AppSettings["returnEmailAddress"];
msg.To = formEmail.Value;
msg.Bcc = ConfigurationSettings.AppSettings["supportEmailAddress"];
msg.Subject = "My message subject";
msg.Body = FormatEmailBody();

SmtpMail.Send( msg );

You would think that would work fine, but it didn’t. At the SmtpMail.Send() command, the following error was thrown:

Server Error in ‘/’ Application.


The transport failed to connect to the server.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.InteropServices.COMException: The transport failed to connect to the server.
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[COMException (0x80040213): The transport failed to connect to the server.]

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) +0
System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) +473
System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) +58

[HttpException (0x80004005): Could not access 'CDO.Message' object.]
System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) +111
System.Web.Mail.CdoSysHelper.Send(MailMessage message) +1861
System.Web.Mail.CdoSysHelper.Send(String from, String to, String subject, String messageText) +72
System.Web.Mail.SmtpMail.Send(String from, String to, String subject, String messageText) +167
MyApp.Web.SignUp.submitButton_Click(Object sender, EventArgs e)
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1273

Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032

I went through all the possible changes…

  • Recently adding the .Net Framework 2.0 to this machine: After re-registering the Web application to use ASP.Net 1.1, I still received the same error (as seen at the bottom of the error message). No problem here.
  • Bad permissions on IIS’s SMTP service: Authentication was anonymous. Relaying was allowed for the local computer. No other restrictions. Sending an email from SQL using CDO worked fine. No problem here
  • Restart services, reboot computer, bang mouse on desk, slam keys on keyboard: Didn’t help.

A quick Google search brought me to the System.Web.Mail Web site. Of all the ideas given in the site, one of them worked. From that Web site:

I have no idea why this suggestion works, but I found it on the web. I figured I would mention it, just in case Suggestion 1 did not work. Instead of specifying
SmtpMail.SmtpServer = “127.0.0.1”
try
SmtpMail.SmtpServer.Insert( 0, “127.0.0.1 or your mail server name here”)

Yes, it worked for me, too, by changing SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["SmtpServer"] to SmtpMail.SmtpServer.Insert( 0, ConfigurationSettings.AppSettings["SmtpServer"] ).

Don’t ask me why, either…