c#:尝试在电子邮件的消息体中发送异常堆栈跟踪

本文关键字:异常 跟踪 堆栈 消息 电子邮件 | 更新日期: 2023-09-27 18:11:20

我写了一个控制台应用程序的登录功能。我正在收到电子邮件,但当有异常时,我在控制台应用程序上获得异常堆栈跟踪,但不在电子邮件中。我在邮件里收到了成功声明。请告诉我这个代码有什么问题…我的意图是,当没有例外时,它应该给出成功消息,当有例外时,它应该在电子邮件中给出详细信息……帮助! !

CA.Selenium

名称空间{

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) => { 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 & 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);

        }
    }
}

}

c#:尝试在电子邮件的消息体中发送异常堆栈跟踪

方法sendEmail对包含异常的message参数不做任何操作。你必须把它插入到邮件正文中。

msg.Body = "Messgage: " + message;