System.Web.Mail FAQ

Complete FAQ Listing

4.1 A Word About Exceptions (READ THIS FIRST) Printer Friendly   Email This FAQ   Discuss

Exceptions generated by System.Web.Mail fall into two categories.
1. Programming/Namespace/DLL exceptions.
2. SMTP Protocol exceptions.

Lets talk about Programming/Namespace/DLL errors. System.Web.Mail is actually a wrapper around the COM dlls CDOSys.dll and CDONTS.dll. (See 1.5. Is System.Web.Mail really a wrapper around the COM CDONTS and CDOSYS? for more information ). Thus, all programming exceptions are really COM related exceptions and are usually generated because there is either a problem accessing the CDOSys.dll or CDONTS.dll, or else these dlls are not installed.

The other exceptions that occur are usually not too obvious, but these are SMTP protocol or network exceptions. When these exceptions are thrown, System.Web.Mail is actually working correctly, but there is either a problem accessing the mail server, or there is a problem relaying through the mail server.

It's important to distinguish between these two exceptions. Unfortunately that is usually easier said than done. The most helpful way to check which type of exception is being thrown is to ITERATE THROUGH ALL INNER EXCEPTIONS. I CANNOT STRESS THIS STRONGLY ENOUGH!! 99% of the time, System.Web.Mail throws the "Could Not Access CDO.Message" exception. This message is not too helpful, however, by checking the Exception.InnerException, some additional helpful text is usually found that will shorten your troubleshooting time CONSIDERABLY.

The following code snippet demonstrates checking the InnerException for more information:
[ C# ]
private void Page_Load(object sender, System.EventArgs e)
	MailMessage mail = new MailMessage();
	mail.To = "";
	mail.From = "";
	mail.Subject = "this is a test email.";
	mail.Body = "Some text goes here";
	SmtpMail.SmtpServer = "";  //will cause an exception to be thrown
		SmtpMail.Send( mail );
	catch(Exception ex )
		Response.Write("The following exception occurred: "  + ex.ToString() );
		//check the InnerException
		while( ex.InnerException != null )
			Response.Write("The following InnerException reported: " + ex.InnerException.ToString() );
			ex = ex.InnerException;

[ VB.NET ]
Private Sub Page_Load(sender As Object, e As System.EventArgs)
   Dim mail As New MailMessage()
   mail.To = ""
   mail.From = ""
   mail.Subject = "this is a test email."
   mail.Body = "Some text goes here"
   SmtpMail.SmtpServer = "" 'will cause an exception to be thrown
   Catch ex As Exception
      Response.Write(("The following exception occurred: " + ex.ToString()))
      'check the InnerException
While Not (ex.InnerException Is Nothing)
   Response.Write(("The following InnerException reported: " + ex.InnerException.ToString()))
ex = ex.InnerException
    End While
   End Try
End Sub 'Page_Load


Discuss This FAQ Item
Got a question? Another Suggestion? Want to post your solution? Discuss it below.
New Post
  • about tracking fake email address    [ Reply ]
  • External component has thrown an exeception    [ Reply ]
  • Mail error    [ Reply ]
  • CDO.Message error    [ Reply ]
  • Inner Exception    [ Reply ]
  • mail thro mailmessage()    [ Reply ]
  • InnerException    [ Reply ]
  • mix of incorrect email addresses    [ Reply ]
  • Email from PDA    [ Reply ]
  • Exception.ToString()    [ Reply ]
  • Could not access cdo.message object    [ Reply ]
  • Object Reference error    [ Reply ]
  • Could not access 'CDO.Message' object.    [ Reply ]
  • send email    [ Reply ]
  • Message could not be sent    [ Reply ]