Example #1
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass app = new ApplicationClass();


            Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(FN, 0, true, 5,
                                                                                  "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
                                                                                  "\t", false, false, 0, true, 1, 0);

            Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;

            int nInLastRow = workSheet.Cells.Find("*", System.Reflection.Missing.Value,
                                                  System.Reflection.Missing.Value, System.Reflection.Missing.Value, Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;


            int   index = 0;
            float ptst  = 0;

            object rowIndex  = 1;
            object colIndex1 = 1;
            object colIndex2 = 2;
            object colIndex3 = 3;

            int total = nInLastRow;

            //try
            //{


            while (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
            {
                rowIndex = 2 + index;

                try
                {
                    string firstName = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
                    string email     = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, colIndex2]).Value2.ToString();
                    string company   = "";
                    string bounce    = "NO";
                    //Thread.Sleep(1);

                    db = new SQLiteDatabase();
                    Dictionary <String, String> data = new Dictionary <String, String>();
                    data.Add("name", firstName);
                    data.Add("email", email);
                    data.Add("companyName", company);
                    data.Add("groupName", selectedList);
                    data.Add("bounce", bounce);

                    db.InsertNoError("tblEmail", data);
                    count2++;
                }
                catch (Exception ex)
                {
                    hasError    = true;
                    errorString = ex.Message;
                }
                index++;
                count++;

                ptst  = count * 100;
                ptst /= total;

                int val = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(ptst)));
                bw1.ReportProgress(val);

                if (isCancelled)
                {
                    e.Cancel = true;
                    break;
                }
                if ((bw1.CancellationPending == true))
                {
                    e.Cancel = true;

                    break;
                }
            }
        }
Example #2
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Int32 overallTotal;

            string tps = "";
            int    cnt = 0;


            overallTotal = cnt + totz;
            int   index = 0;
            int   total = overallTotal;
            int   count = 0;
            float ptst  = 0;

            tt = 0;

            #region ExcelImport ---
            if (fileName != "" && fileName.Contains(".xls"))
            {
                Microsoft.Office.Interop.Excel.ApplicationClass app      = new ApplicationClass();
                Microsoft.Office.Interop.Excel.Workbook         workBook = app.Workbooks.Open(fileName, 0, true, 5,
                                                                                              "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
                                                                                              "\t", false, false, 0, true, 1, 0);
                Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;
                int nInLastRow = workSheet.Cells.Find("*", System.Reflection.Missing.Value,
                                                      System.Reflection.Missing.Value, System.Reflection.Missing.Value, Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;



                object rowIndex  = 1;
                object colIndex1 = 1;
                object colIndex2 = 2;

                overallTotal += nInLastRow;

                total = overallTotal;


                string newtext = "";

                string message = mymessage;
                while (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
                {
                    rowIndex = 2 + index;
                    PTS      = "";

                    try
                    {
                        string firstName = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
                        string email     = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, colIndex2]).Value2.ToString();

                        if (SendMail(email, message, globalSubject, fromName, ""))
                        {
                            newtext = firstName + ", " + email + " --> Email Sent!\r\n";
                            tt     += 1;
                        }
                        else
                        {
                            newtext = firstName + ", " + email + " --> Email Failed!\r\n";
                            listBox1.Items.Add(email);
                        }
                    }
                    catch (Exception ex)
                    {
                        //PTS += @"\r\nError Occured : Index " + index.ToString() + " --> " + ex.Message.ToString() + @"\r\n";
                    }
                    index++;
                    count++;

                    ptst  = count * 100;
                    ptst /= total;

                    int val = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(ptst)));
                    bw1.ReportProgress(val, newtext);
                }
            }
            #endregion

            #region EmailList

            if (totz > 0)
            {
                //try
                //{
                db = new SQLiteDatabase();
                System.Data.DataTable recipe;
                String query = "select * from tblemail where groupname = '" + smtpTitle + "'";
                recipe = db.GetDataTable(query);

                total = totz;
                count = 0;
                index = 0;
                ptst  = 0;

                string newText = "";

                foreach (DataRow r in recipe.Rows)
                {
                    PTS = "";
                    string em = "", nm = "";
                    em = r["email"].ToString();
                    nm = r["name"].ToString();

                    try
                    {
                        if (SendMail(r["email"].ToString(), mymessage, globalSubject, fromName, ""))
                        {
                            newText = r["name"].ToString() + ", " + r["email"].ToString() + " --> Email Sent!\r\n";
                        }
                        else
                        {
                            newText = r["name"].ToString() + ", " + r["email"].ToString() + " --> Email Failed!\r\n";
                            listBox1.Items.Add(r["email"].ToString());
                        }
                    }

                    catch (Exception ex)
                    {
                        // newText += @"\r\nError Occured : Index " + index.ToString() + " --> " + ex.Message.ToString() + @"\r\n";
                        newText = "";
                    }

                    tt += 1;
                    index++;
                    count++;

                    ptst  = count * 100;
                    ptst /= total;

                    PTS = em + "\r\n";

                    int val = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(ptst)));
                    bw1.ReportProgress(val, newText);
                }


                //}
                //catch (Exception fail)
                //{
                //    PTS += @"\r\n" + fail.Message.ToString() + "\r\n";

                //}
            }

            #endregion

            #region ManualEnter

            if (totList != "")
            {
                try
                {
                    tps = totList;

                    string[] words = tps.Split(',');

                    total = words.Length;
                    count = 0;
                    string newtext = "";

                    foreach (string word in words)
                    {
                        if (SendMail(word.Trim(), mymessage, globalSubject, fromName, ""))
                        {
                            newtext = word.Trim() + ", " + word.Trim() + " --> Email Sent!\r\n";
                            tt     += 1;
                        }
                        else
                        {
                            newtext = word.Trim() + ", " + word.Trim() + " --> Email Failed!\r\n";
                            listBox1.Items.Add(word.Trim());
                        }


                        index++;
                        count++;

                        ptst  = count * 100;
                        ptst /= total;

                        int val = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(ptst)));
                        bw1.ReportProgress(val, newtext);
                    }
                }
                catch (Exception op)
                {
                    PTS += @"\r\n" + op.Message + @"\r\n";
                }
            }

            #endregion
        }