Exceptions generated by System.Web.Mail fall into two categories.
1. Programming/Namespace/DLL exceptions.
and
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 = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "Some text goes here";
SmtpMail.SmtpServer = "mail.fake_domain.com"; //will cause an exception to be thrown
try
{
SmtpMail.Send( mail );
}
catch(Exception ex )
{
Response.Write("The following exception occurred: " + ex.ToString() );
//check the InnerException
while( ex.InnerException != null )
{
Response.Write("--------------------------------");
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 = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "this is a test email."
mail.Body = "Some text goes here"
SmtpMail.SmtpServer = "mail.fake_domain.com" 'will cause an exception to be thrown
Try
SmtpMail.Send(mail)
Catch ex As Exception
Response.Write(("The following exception occurred: " + ex.ToString()))
'check the InnerException
While Not (ex.InnerException Is Nothing)
Response.Write("--------------------------------")
Response.Write(("The following InnerException reported: " + ex.InnerException.ToString()))
ex = ex.InnerException
End While
End Try
End Sub 'Page_Load