Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Emails Extractor

, 30 Sep 2012 CPOL
Program Extracts Emails and Data (names,phones and Gender) belongs to a group of people from text file and order them into Datagridview which can be saved as .csv file contains people’s contacts which can be imported to a yahoo mail account.
email_extractor_source-noexe.zip
WindowsFormsApplication9
EmailsExtractor.suo
WindowsFormsApplication9
Emails_Extractor.csproj.user
obj
x86
Debug
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
WindowsFormsApplication9.MainForm.resources
WindowsFormsApplication9.Properties.Resources.resources
Properties
Settings.settings
RegexDatabase.sdf
RegexDatabaseDataSet.xsc
RegexDatabaseDataSet.xss
email_extractor_source.zip
EmailsExtractor.suo
Emails_Extractor.csproj.user
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
TempPE
RegexDatabase.Designer.cs.dll
RegexDatabaseDataSet.Designer.cs.dll
RegexDatabaseDataSetcon.Designer.cs.dll
WindowsFormsApplication9.exe
WindowsFormsApplication9.MainForm.resources
WindowsFormsApplication9.Properties.Resources.resources
Settings.settings
RegexDatabase.sdf
RegexDatabaseDataSet.xsc
RegexDatabaseDataSet.xss
/*
"Message Before Code"
 * Alslamo Alikom Wa Rahmato Allah Wa Baraktoh(Peace be upon you and God's mercy and blessings)
 * Dear All,
Islam is the religion of mercy and compassion and what has happened in Libya is barbaric, brutal and irresponsible act, represents only the category who has done and doesn’t represent Islamic or Arabic nation. 
I am sincerely offering my sympathy to the family of the American ambassador and U.S. people. 
May God grant his family compassion and courage, especially in the eleventh anniversary of 11 September disaster, in which many innocent people has been killed.
                                                           By Al-Samman Mahmoud
                                                               A true Muslim  

*/

/*
 * This Code represents an idea I had about adding many people to my Email book in Yahoo Manssenger , 
 * I faced the fact that adding all these people taking more time and effort
 * So i disscovered that u can import contacts by adding them in specific CSV file with specific columns and then iporting this file 
 * to Ur contacts
 * By following Steps :
 * 1- slect contacts from Ur Mail box
 * 2-Create new Mail list
 * 3-add choose ur new list
 * 4-choose import contacts
 * 5-choose other 
 * 6-unselect "other Yahoo contacts"
 * 7-select " A desktop email program (Outlook, AppleMail, etc...)"
 * 8-Upload Ur file (.csv or othe type)
 * 9-select "I give Yahoo! permission to send my..."
 * 10-press continue
 * 11-choose the contacts u need to add
 * */





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Collections;

namespace WindowsFormsApplication9
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        bool NoDataWithoutEmail { get { if (NoEmailscheckBox.Checked) { return true; } else { return false; };} }
        private void Form1_Load(object sender, EventArgs e)
        {
            
            DataTable A = new DataTable("Samman");
            string[] Columnsarray = PersonData.DataColumns;
            for (int i = 0; i < Columnsarray.Length; i++)
            {
                A.Columns.Add(Columnsarray[i].ToString());
            }
            EmailsdataGridView.DataSource = A;
            LoadRegexGridView();

            LoadGetGroupsinComboBox();
             
        }
        RegexDatabaseDataSetTableAdapters.RegexGroupsTableTableAdapter GroupsTables;

        private void LoadGetGroupsinComboBox()
        {
            RegexGroupscomboBox.Items.Clear();
            GroupsTables = new RegexDatabaseDataSetTableAdapters.RegexGroupsTableTableAdapter();
            foreach (var item in GroupsTables.GetGroupsNames())
            {
                RegexGroupscomboBox.Items.Add(item["groupname"]);
            } 
        }
        private int GetGroupId(string Nam)
        {
            GroupsTables = new RegexDatabaseDataSetTableAdapters.RegexGroupsTableTableAdapter();
            int GID = GroupsTables.GetGroupID(Nam).Value;
            return (int)GID;
        }

       
        private int GetMaxGroupID()
        {
            GroupsTables = new RegexDatabaseDataSetTableAdapters.RegexGroupsTableTableAdapter();
            int GID = GroupsTables.GetMaxID().Value;
            
            return GID;
        }
         
        private void LoadRegexGridView()
        {
            DataGridViewComboBoxColumn ResultCount = new DataGridViewComboBoxColumn();
            DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
            DataTable RegexTable = new DataTable("Samman");
            string[] Columnsarray = {  "Regex Expression"};

            for (int i = 0; i < Columnsarray.Length; i++)
                RegexTable.Columns.Add(Columnsarray[i].ToString());
            
            ResultCount.HeaderText = "Number of Result";
            ResultCount.Name = "Number of Result";
            ResultCount.MaxDropDownItems = 4;
            ResultCount.Items.Add("1");
            ResultCount.Items.Add("2");
            ResultCount.Items.Add("3");
            ResultCount.Items.Add("All");
            cmb.HeaderText = "Coulmn Target";
            cmb.Name = "Coulmn Target";
            int DataEnumCount = Enum.GetNames(typeof(Person.DataType)).Count();
            cmb.MaxDropDownItems = DataEnumCount;
           // cmb.Items.AddRange(Enum.GetNames(typeof(DataType)));
            for (int i = 0; i < EmailsdataGridView.Columns.Count; i++)
                 cmb.Items.AddRange(EmailsdataGridView.Columns[i].Name);
           RegexDataGridView.DataSource = RegexTable;
            RegexDataGridView.Columns.Add(cmb);
            RegexDataGridView.Columns.Add(ResultCount);
        }

        private void LoadRegexGridView(List<string>Regex)
        {
            DataGridViewComboBoxColumn ResultCount = new DataGridViewComboBoxColumn();
            DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
            DataTable RegexTable = new DataTable("Samman");
            string[] Columnsarray = { "Regex Expression" };

            for (int i = 0; i < Columnsarray.Length; i++)
                RegexTable.Columns.Add(Columnsarray[i].ToString());

            ResultCount.HeaderText = "Number of Result";
            ResultCount.Name = "Number of Result";
            ResultCount.MaxDropDownItems = 4;
            ResultCount.Items.Add("1");
            ResultCount.Items.Add("2");
            ResultCount.Items.Add("3");
            ResultCount.Items.Add("All");
            cmb.HeaderText = "Coulmn Target";
            cmb.Name = "Coulmn Target";
            int DataEnumCount = Enum.GetNames(typeof(Person.DataType)).Count();
            cmb.MaxDropDownItems = DataEnumCount;
            // cmb.Items.AddRange(Enum.GetNames(typeof(DataType)));
            for (int i = 0; i < EmailsdataGridView.Columns.Count; i++)
                cmb.Items.AddRange(EmailsdataGridView.Columns[i].Name);
            for (int i = 0; i < Regex.Count; i++)
            {
                DataRow Row = RegexTable.NewRow();
                Row["Regex Expression"] = Regex[i];
                RegexTable.Rows.Add(Row);
            }
            RegexDataGridView.DataSource = RegexTable;
            RegexDataGridView.Columns.Add(cmb);
            RegexDataGridView.Columns.Add(ResultCount);
        }
        
        private void ExtractDataToCSV(DataGridView dgv)
        {

            // Don't save if no data is returned
            if (dgv.Rows.Count == 0)
            {
                return;
            }
            StringBuilder sb = new StringBuilder();
            // Column headers
            string columnsHeader = "";
            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                columnsHeader += dgv.Columns[i].Name + ",";
            }
            sb.Append(columnsHeader + Environment.NewLine);
            // Go through each cell in the datagridview
            foreach (DataGridViewRow dgvRow in dgv.Rows)
            {
                // Make sure it's not an empty row.
                if (!dgvRow.IsNewRow)
                {
                    for (int c = 0; c < dgvRow.Cells.Count; c++)
                    {
                        // Append the cells data followed by a comma to delimit.

                        sb.Append(dgvRow.Cells[c].Value + ",");
                    }
                    // Add a new line in the text file.
                    sb.Append(Environment.NewLine);
                }
            }
            // Load up the save file dialog with the default option as saving as a .csv file.
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "CSV files (*.csv)|*.csv";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // If they've selected a save location...
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName, false))
                {
                    // Write the stringbuilder text to the the file.
                    sw.WriteLine(sb.ToString());
                }
            }
            // Confirm to the user it has been completed.
            MessageBox.Show("CSV file saved.");
        }

        private void saveYahooEmailBookToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExtractDataToCSV(EmailsdataGridView);
        }

        private void openMixedTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlgOpen = new OpenFileDialog())
            {
                try
                {
                    // Available file extensions
                    dlgOpen.Filter = "All files(*.*)|*.*";
                    // Initial directory
                    dlgOpen.InitialDirectory = "D:";
                    // OpenFileDialog title
                    dlgOpen.Title = "Open";
                    // Show OpenFileDialog box
                    if (dlgOpen.ShowDialog() == DialogResult.OK)
                    {
                        // Create new StreamReader
                        StreamReader sr = new StreamReader(dlgOpen.FileName, Encoding.Default);
                        // Get all text from the file
                        string str = sr.ReadToEnd();
                        // Close the StreamReader
                        sr.Close();
                        // Show the text in the rich textbox rtbMain
                        
                        MixedEmailSRichtextBox.Text = str;
                    }
                }
                catch (Exception errorMsg)
                {
                    MessageBox.Show(errorMsg.Message);
                }
            }

      
        }

        /*These collection of rgerxe were revealed Page "This tour-de-force yiWK program reveals the strengths and weaknesses of the 
         * rule-based paradigm and will prove useful for 
         mailing-list programmers"
         PREDICT GENDER GIVEN A FIRST NAME by Scott Pakin, August 1991 lawker.googlecode.com/svn/fridge/share/pdf/pakin1991.pdf
        */
        
        static List<Person> GetPeopleFromBlock(string Data,string RegexSplitting)
        {
            List<Person> MyPData = new List<Person>();
            try
            {
                string[] M = Regex.Split(Data, RegexSplitting); ;

                foreach (var Pdata in M)
                {
                    if (Pdata.Length > 20)
                    {
                        Person newPerson = new Person(Pdata);

                        MyPData.Add(newPerson);
                    }
                }
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
            }
            
            return MyPData;
        }
        static bool NoEmailNoData = true;
        static List<int> WrongRegex = new List<int>();



        private DataTable PeopleTable(List<Person> People, List<AdditionalRegexExpression> AdditionalRegex)
        {

            DataTable PeopleTable = new DataTable();
            AddCoulmnsForPeopleTable(PeopleTable);
            for (int i = 0; i < People.Count; i++)
            {
                DataRow NewPerson = PeopleTable.NewRow();
                List<PersonData> newPErsonData =People[i].GetPersonFromBlock(AdditionalRegex,NoDataWithoutEmail);
                for (int P = 0; P < newPErsonData.Count; P++)
                {
                    if (newPErsonData[P].dataType == null)
                        continue;
                    string ColumnName = newPErsonData[P].dataType.ToString().Replace('_', ' ');
                    if(ColumnName=="Email Address")
                        ColumnName="E-mail Address";
                    if (ColumnName == "Email Display Name")
                        ColumnName = "E-mail Display Name";
                string AlreadyExistedData = NewPerson[ColumnName].ToString();//if there are existed data already
                NewPerson[ColumnName] = AlreadyExistedData + CleanString(newPErsonData[P].Data.ToString());//in case if data contain ',' this will confuse excel program 
                }
                if(newPErsonData.Count>0)
                  PeopleTable.Rows.Add(NewPerson);
            }
            return PeopleTable;
        }
        static string CleanString(string X)
        {
            
           return  Regex.Replace(X,RegexExpressionCollection.CleanStringForExcel," "); 
        }
        private void AddCoulmnsForPeopleTable(DataTable PeopleTable)
        {

            string[] Columnsarray = PersonData.DataColumns; 
            for (int i = 0; i < Columnsarray.Length; i++)
            {
                PeopleTable.Columns.Add(Columnsarray[i].ToString());
            }
        
        }
       
        private void buttonSearch_Click(object sender, EventArgs e)
        {
            SearchForPeople();
        }
        private void SearchForPeople()
        {
            WrongRegex.Clear();
            string Block = MixedEmailSRichtextBox.Text;//control contain text data for different people
            string Regex = @"(\n){2,}(\w)*?";
            if (BlockSplitingText.Enabled == true && BlockSplitingText.Text.Length > 0)
                Regex = BlockSplitingText.Text;
            if (BlockSplitingText.Enabled == true && BlockSplitingText.Text.Length > 0)
                Regex = BlockSplitingText.Text;
            List<Person> People = GetPeopleFromBlock(Block, Regex);
            this.EmailsdataGridView.DataSource = PeopleTable(People, GetAdditionalRegexfrom(RegexDataGridView));
        
        }
        private List<AdditionalRegexExpression> GetAdditionalRegexfrom(DataGridView GridView)
        {
            List<AdditionalRegexExpression> MyRegexList = new List<AdditionalRegexExpression>();
            
            for (int i = 0; i < GridView.Rows.Count; i++)
            {
              int NumOfResult=0;
              if (GridView[1, i].Value == null || GridView[2, i].Value==null)
                  break;
              if(GridView[2,i].Value.ToString().ToLower()=="all")
                 NumOfResult=0;
              else
                 NumOfResult =(int)GridView[2,i].Value;
              MyRegexList.Add(new AdditionalRegexExpression(GridView[1,i].Value.ToString(),GridView[0,i].Value.ToString(),NumOfResult));
            }
            return MyRegexList;
        
        }
        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked == true)
                BlockSplitingText.Enabled = true;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked == true)
                BlockSplitingText.Enabled = false;
        }
     


        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }
        RegexDatabaseDataSetTableAdapters.RegexExpressionsTableTableAdapter RegexTable;
        private void SaveToDataBase(DataGridView MyGridView,string Groupname)
        {
      
            if(IsGroupNameExist(Groupname))
            {
                MessageBox.Show("This Group Name is Exist !!!");
                return;
            }
            int MaxId = GetMaxGroupID();
            RegexTable = new RegexDatabaseDataSetTableAdapters.RegexExpressionsTableTableAdapter();
            for (int i = 0; i < MyGridView.Rows.Count; i++)
            {
                if (MyGridView[0, i].Value == null)
                    break;
                RegexTable.InsertNewRegex((byte)(MaxId + 1), MyGridView[0, i].Value.ToString());
            }
          GroupsTables.InsertNewGroup(Groupname,(byte)(MaxId+1));
        
        }
        private bool IsGroupNameExist(string Name)
        {
            GroupsTables = new RegexDatabaseDataSetTableAdapters.RegexGroupsTableTableAdapter();
            object GNam = GroupsTables.SelectRegexGroup(Name);
            if (GNam == null)
                return false;
            else
                return true;
        
        }
        private void button1_Click(object sender, EventArgs e)
        {
            SaveToDataBase(this.RegexDataGridView, NewRegexGroups.Text);
            LoadGetGroupsinComboBox();
            NewRegexGroups.Enabled = false;
        }

        private void loadNewRegexGroup(string GroupNam)
        {

            RegexDataGridView.Columns.Clear();

            int IDG = GetGroupId(RegexGroupscomboBox.SelectedItem.ToString());
            List<string> Regexes = RegexExp(IDG);
           
                RegexDataGridView.DataSource = null;
                RegexDataGridView.Refresh();
                LoadRegexGridView(Regexes);
           

        }

        
        private List<string> RegexExp(int IDGroups)
        {
            RegexTable = new RegexDatabaseDataSetTableAdapters.RegexExpressionsTableTableAdapter();
            List<string> RegexExp = new List<string>();
            foreach (var item in RegexTable.GetRegexByGroupID((byte)IDGroups))
            {
                RegexExp.Add((string)item["RegexExp"]);
            }
            return RegexExp;
        }
        private void RegexGroupscomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadNewRegexGroup(RegexGroupscomboBox.SelectedItem.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            NewRegexGroups.Enabled = true;
            
        }

        private void MainMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void NoEmailscheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (NoEmailscheckBox.Checked == true)
                NoEmailNoData = false;
            else
                NoEmailNoData = true;
        }

        private void saveMixedTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog Sv = new SaveFileDialog();
            Sv.Filter =
          "Gene Bank File (*.rtf) | *.rtf";
            if (Sv.ShowDialog()==DialogResult.OK)
            {
                this.MixedEmailSRichtextBox.SaveFile(Sv.FileName);
            }
        }
        void Remove()
        {
           
        
        }

        private void RemovetextBox_MouseHover(object sender, EventArgs e)
        {
        }

        private void RepradioButton_CheckedChanged(object sender, EventArgs e)
        {
            AddgroupBox.Enabled = false;
            TexttextBox.Enabled = false;
        }

        private void AddradioButton_CheckedChanged(object sender, EventArgs e)
        {
            AddgroupBox.Enabled = true;
            TexttextBox.Enabled = true;
        }

        private void ReplaceradioButton_CheckedChanged(object sender, EventArgs e)
        {
            TexttextBox.Enabled = true;
            AddgroupBox.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (RemoveradioButton.Checked)
                {

                    this.MixedEmailSRichtextBox.Text = Regex.Replace(this.MixedEmailSRichtextBox.Text, RegexMixedExecutetextBox.Text, "");
                }
                else if (ReplaceradioButton.Checked)
                {
                    this.MixedEmailSRichtextBox.Text = Regex.Replace(this.MixedEmailSRichtextBox.Text, RegexMixedExecutetextBox.Text, TexttextBox.Text);
                }
                else if (AddradioButton.Checked)
                {
                    MatchCollection Matches = Regex.Matches(MixedEmailSRichtextBox.Text, RegexMixedExecutetextBox.Text);
                    for (int i = 0; i < Matches.Count; i++)
                    {
                        string newText = "";
                        if (BeforeradioButton.Checked)
                            newText = TexttextBox.Text + Matches[i].Value.ToString();
                        if (AfterradioButton.Checked)
                            newText = Matches[i].Value.ToString() + TexttextBox.Text;
                        if (BeforeradioButton.Checked || AfterradioButton.Checked)
                            MixedEmailSRichtextBox.Text = Regex.Replace(this.MixedEmailSRichtextBox.Text, Matches[i].Value.ToString(), newText);
                        else
                            MessageBox.Show("please select if the addition text will be before or after");
                    }
                }
                else
                    MessageBox.Show("please select which oberation will be executed !!");
            }
            catch (Exception d)
            {

                MessageBox.Show(d.Message);
            }
          
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.MixedEmailSRichtextBox.Text = Regex.Replace(this.MixedEmailSRichtextBox.Text, "\n", "");
             
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Al-Samman Mahmoud
Software Developer Agriculture Genetic Engineering Research Institute
Egypt Egypt
I have adored bioinformatics since my second year in college and then I specialized in this field in my final year in biotechnology department, I have learned both C# and Perl Languages to build open soft-wares contain specialized tools in this particular science.
I'm trying to enhance my knowledge in this beautiful field by both reading and writing articles , also I hope to gain both master and doctoral degree in it.
If you have any scholarship, source , opportunity , project or idea which can help me to reach my goal don't hesitate to contact me on :
E-Mail:samman_mahmoud@yahoo.com
Facebook:Samman Mahmoud
Tel: +20118904500
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Oct 2012
Article Copyright 2012 by Al-Samman Mahmoud
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid