public PreviewWindow(EmailObject emailToPreview)
        {
            previewEmailObj = emailToPreview;
            InitializeComponent();
            loadPreview();

            // DEBUG ListBox
            addressListBox.SelectionMode = SelectionMode.Multiple;
            for (int test = 500600; test < 500750; test++)
            {
                addressListBox.Items.Add(test.ToString());
            }
            addressListBox.SelectAll();
        }
        private void startNewBatch()
        {
            int sentMsgLimit;
            int messagesSent = 0;
            string stringMsgLimit = limitTextbox.Text;
            bool hitLimit = false;

            // Get the limit on the number of messages to be sent in a session
            if (stringMsgLimit == null || stringMsgLimit.Equals(""))
            {
                sentMsgLimit = Int32.MaxValue;
            }
            else
            {
                sentMsgLimit = Int32.Parse(limitTextbox.Text);
            }

               /* if (mailSent > 0 )
            {
                // We sent some mail last time, so lower the limit
                sentMsgLimit
            }*/

            statusText.Text = "Starting new email batch ...";
            statusText.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (Action)delegate() { });
            bool failureOccurred = false;
            String addressLoc = "", bodyLoc="", subject="", attachmentLoc="", currentDir="";
            gatherEmailStrings(ref addressLoc, ref bodyLoc, ref subject,
                                ref attachmentLoc, ref currentDir);
            statusText.Text = "Preparing logs for batch ...";
            statusText.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (Action)delegate() { });
            // Check to see if there is a log folder
            if (!Directory.Exists(currentDir + LOG_DIR))
            {
                // Make the directory
                Directory.CreateDirectory(currentDir + LOG_DIR);
            }
               // Initiate a new text file to log any issues
            statusText.Text = "Initializing error logs ...";
            statusText.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (Action)delegate() { });
            String logFilePath = currentDir + LOG_DIR + "\\error_log_" + DateTime.Now.ToString().Replace(".", "_").Replace(":", "_").Replace("/", "_") + ".txt";
            System.IO.StreamWriter logFile = new System.IO.StreamWriter(logFilePath);
            logFile.WriteLine(LOG_START);
            logFile.WriteLine("****************************");

            // Create the excel sheet
            ExcelObject excelObj = new ExcelObject(addressLoc);

            statusText.Text = "Loading recepient addresses...";
            statusText.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (Action)delegate() { });
            // Create the dictionary for the addresses
            Dictionary<int, Utilities.ExcelRowBinder> allAddresses = excelObj.getAddresses();
            if (allAddresses.Count == 0)
            {
                MessageBox.Show("The current email cannot be sent.", "Sorry");
                subjectTextbox.Text = "";
                sendButton.IsEnabled = false;
                return;
            }
            currentStatus.setObjectiveTotal(allAddresses.Count);
            currentStatus.setStatus("Sending Messages");
            statusText.Text = currentStatus.getObjectiveStatus();
            statusText.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (Action)delegate() { });
            sendingProgressBar.Maximum = allAddresses.Count;

            int nextRowSent = 0;
            string nextAddressSent = "";
            // Loop through addresses and make a new email for each one
            foreach (int keyIndex in allAddresses.Keys)
            {
                if (messagesSent < sentMsgLimit)
                {
                    String newAddr;
                    Utilities.ExcelRowBinder currentBinder;

                    // Need to extract the email value from the ExcelRowBinder

                    if (allAddresses.TryGetValue(keyIndex, out currentBinder) == true)
                    {
                        newAddr = currentBinder.getValue();
                        EmailObject newEmail = new EmailObject(attachmentLoc, bodyLoc, newAddr, subject);
                        try
                        {
                            newEmail.sendEmail();
                            try
                            {
                                currentStatus.incrementCurrentObjective();
                                statusText.Text = currentStatus.getObjectiveStatus();
                                statusText.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (Action)delegate() { });
                                excelObj.logSuccess(currentBinder.getRowNumber());
                            }
                            catch (Exception logException)
                            {
                                // Do nothing
                            }

                        }
                        catch (Exception e)
                        {
                            // There was some reason the email could not be sent.  Log those errors
                            logFile.WriteLine("SEND FAILED: " + newAddr);
                            failureOccurred = true;

                        }

                        sendingProgressBar.Value++;
                        sendingProgressBar.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (Action)delegate() { });
                    }
                }
                else
                {
                    // We've hit the limit, time to stop this
                    hitLimit = true;
                    Utilities.ExcelRowBinder currentBinder;
                    if (allAddresses.TryGetValue(keyIndex, out currentBinder) == true)
                    {
                        nextRowSent = currentBinder.getRowNumber();
                        nextAddressSent = currentBinder.getValue();
                    }
                    //lastRowSent =
                    break;
                }
                messagesSent++;

            }

            string sessionInfoPath = currentDir + "\\StoredSessionInfo.txt";
                System.IO.StreamWriter sessionInfo = new System.IO.StreamWriter(sessionInfoPath, false); // Overwrite old info
            if (!hitLimit)
            {
                // Alert the user that the task is done
                MessageBox.Show("All emails have been sent!", "Attention!");
                if (!failureOccurred)
                {
                    logFile.WriteLine("ALL MESSAGES SENT SUCCESSFULLY!!");
                }

                // Overwrite the session info
                writeSessionInfo(sessionInfo, "N", "", 0, "", "", "", "", 0);
            }
            else
            {
                MessageBox.Show("The maximum number of emails have been sent for this session", "Attention");
                logFile.WriteLine("THE MAXIMUM {0} MESSAGES HAVE BEEN SENT!!", sentMsgLimit);

                // Amend the StoredSessionInfo.txt file

                writeSessionInfo(sessionInfo, "Y", addressLoc, nextRowSent, nextAddressSent, bodyLoc, attachmentLoc, subject, messagesSent);
            }
            sessionInfo.Close();
            sendingProgressBar.Visibility = System.Windows.Visibility.Hidden;
            logFile.Close();
            excelObj.saveAndClose();

            MessageBox.Show("All logs have been saved and closed", "Attention!");

            subjectTextbox.Clear();
            currentStatus.setStatus("");
            statusText.Text = "";
        }
        private void previewButton_Click(object sender, RoutedEventArgs e)
        {
            String addressLoc = "", bodyLoc = "", subject = "", attachmentLoc = "", currentDir = "";
            gatherEmailStrings(ref addressLoc, ref bodyLoc, ref subject,
                                ref attachmentLoc, ref currentDir);

            EmailObject previewEmail = new EmailObject(attachmentLoc, bodyLoc, "*****@*****.**", subject);

            PreviewWindow emailPreviewWindow = new PreviewWindow(previewEmail);
            emailPreviewWindow.Show();
        }