title: C#: Trying to send the exception stack trace in the message body of the email.
body: <p>I have written a console app for login function. I am recieving the email but when there is an exception I get the exception stack Trace on the console app but not in the email. I get the success statement in the email. PLease tell me what is wrong with this code... My intention is when there is no exception it should give the success message and when there is exception it should give the details in the email... Help!!!</p>
  
  <p>namespace CA.Selenium
  {</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          string m_ErrorMessage = "";
  
  
          int m_ErrorCode = 0;
          System.Console.WriteLine("Loading xxxxxx Remote Login Monitor");
  
          InternetExplorerDriver driver = new InternetExplorerDriver(@"C:\Users\xxxx\xxxx\IEDriverServer_x64_2.32.1");
          System.Console.WriteLine("Loading IE Browser");
          try
          {
  
              //Navigate to the URL
              System.Console.WriteLine("Browsing to http://xxxxxxxx.com");
              driver.Navigate().GoToUrl("http://xxxxxxxxxx.com");
  
              System.Console.WriteLine("Attempting a Login");
              // Find the text input element by its name
              IWebElement queryLogin = driver.FindElement(By.Name("menuheader$btnLogin"));
              queryLogin.Click();
  
              IWebElement queryUserName = driver.FindElement(By.Name("menuheader$ContextLogin$textUserName"));
              // Enter something to search for
              queryUserName.SendKeys("xxxxxx");
  
              IWebElement queryPassword = driver.FindElement(By.Name("menuheader$ContextLogin$textPassword"));
              // Enter something to search for
              queryPassword.SendKeys("xxxxxxxx");
  
              IWebElement queryLoginSubmit = driver.FindElement(By.Name("menuheader$ContextLogin$btnLogin"));
              // Find the control by its name and click
              queryLoginSubmit.Click();
  
              // Wait for the page to load, timeout after 10 seconds and find the control ID on the new page
              System.Console.WriteLine("Waiting 10 seconds for Login to finish before throwing an error.");
              WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
              wait.Until((d) =&gt; { return d.FindElement(By.Id("menuheader_MyAccountLink")); });
  
              // If successful.
              System.Console.WriteLine("Page title rendered is: " + driver.Title);
              System.Environment.ExitCode = m_ErrorCode;
  
              //System.Console.WriteLine("Sending Email...");
              //throw new System.InvalidOperationException();
              Program.sendEmail(m_ErrorMessage);
  
  
          }
          catch (Exception ex)
          {
              //log exception
              //System.Console.WriteLine("An error was found. ");
              Console.WriteLine("Exception caught: {0} ", ex.ToString());
              System.Environment.ExitCode = 1;
              m_ErrorMessage = "Exception caught: {0} " + ex.Message.ToString();
              System.Console.WriteLine(ex.StackTrace);
              //System.Console.WriteLine("Sending Email...");
              Program.sendEmail(m_ErrorMessage);
          }
  
          finally
          {
              System.Console.WriteLine("Shutting down xxxxxx Remote Login Monitor");
              //Close the browser &amp; dispose
              driver.Close();
              driver.Quit();
              driver.Dispose();
          }
  
      }
  
  
      public static void sendEmail(string message)
      {
          string smtpServer = System.Configuration.ConfigurationManager.AppSettings["smtpServer"];
          MailAddress from = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["FromEmailAddress"]);
          MailAddress to = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["ToEmailAddress"]);
          MailMessage msg = new MailMessage(from, to);
          msg.Subject = "Login ";
          msg.Body = "Successfully Passed. No Exceptions Found.";
          SmtpClient client = new SmtpClient();
          client.Host = smtpServer;
          Console.WriteLine("Sending an e-mail message to {0} by using SMTP host {1}.", to.ToString(), client.Host);
  
          try
          {
              msg.To.Add(new MailAddress(System.Configuration.ConfigurationManager.AppSettings["ToEmailAddress"]));
              client.Send(msg);
  
  
          }
          catch (Exception ex)
          {
              Console.WriteLine("Exception caught: {0}", ex.ToString());
              msg.Body = ex.StackTrace.ToString();
              //System.Console.WriteLine(ex.StackTrace);
  
  
          }
  
      }
  }
  </code></pre>
  
  <p>}</p>