1

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

命名空间 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);


        }

    }
}

}

4

1 回答 1

2

该方法sendEmail不对message包含异常的参数做任何事情。您必须将其插入电子邮件正文中。

msg.Body = "Messgage: " + message;
于 2013-04-29T22:14:02.170 回答