<<Back To Faq 5


Complete FAQ Listing


5 Troubleshooting System.Net.Mail Printer Friendly

System.Net.Mail seems like a pretty stable class of email objects. There are a few known issues. I'm sure more will crop up over time. At the time I put this FAQ together, System.Net.Mail has been out of beta for less than 30 days. Check the forums for more ideas and help.

If you do get any exceptions, be sure to always check the inner exception for more additional information. Usually the inner exceptions will provide the additional information to solve your problem. Here's a code example for checking inner exceptions in a console application. If you want to use this code in an ASP.NET page, be sure to change Console.WriteLine(...) to Response.Write(...).

[ C# ]
 public static void InnerExceptions()
 {
     //create the mail message
     MailMessage mail = new MailMessage();

     //set the addresses
     mail.From = new MailAddress("me@mycompany.com");
     mail.To.Add("him@hiscompany.com");

     //set the content
     mail.Subject = "This is an email";
     mail.Body = "this is the body content of the email.";

     //send the message
     SmtpClient smtp = new SmtpClient("127.0.0.1");
     try
     {
         smtp.Send(mail);
     }
     catch (Exception ex)
     {
         Exception ex2 = ex;
         string errorMessage = string.Empty;
         while (ex2 != null)
         {
             errorMessage += ex2.ToString();
             ex2 = ex2.InnerException;
         }

         Console.WriteLine(errorMessage);
     }
 }



[ VB.NET ]
Public Shared Sub InnerExceptions()
   'create the mail message
   Dim mail As New MailMessage()
   
   'set the addresses
   mail.From = New MailAddress("me@mycompany.com")
   mail.To.Add("him@hiscompany.com")
   
   'set the content
   mail.Subject = "This is an email"
   mail.Body = "this is the body content of the email."
   
   'send the message
   Dim smtp As New SmtpClient("127.0.0.1")
   Try
      smtp.Send(mail)
   Catch ex As Exception
      Dim ex2 As Exception = ex
      Dim errorMessage As String = String.Empty
      While Not (ex2 Is Nothing)
         errorMessage += ex2.ToString()
         ex2 = ex2.InnerException
      End While
      
      Console.WriteLine(errorMessage)
   End Try
End Sub 'InnerExceptions

 



The formatted version of this faq can be found at http://www.SystemNetMail.com/faq/5.aspx