Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using Excel = Microsoft.Office.Interop.Excel;
using word = Microsoft.Office.Interop.Word;

namespace E_mailExtractor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnText_Click(object sender, EventArgs e)
        {
            string path = Application.StartupPath + @"\\E-mail-Text.txt";
            TextWriter tw = new StreamWriter(path);
            tw.Close();
            //else if (File.Exists(path))
            //    {
            //   TextWriter tw = new StreamWriter(path);
            //   tw.WriteLine("The next line!");
            //   tw.Close(); 
            //            }
            //StreamWriter tw11 = new StreamWriter("j.txt");
            //tw11.WriteLine("hiiiiiiiiii");
            //char ch;
            //int Tchar = 0;
            //int size = -1;
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;
                try
                {
                    string text = File.ReadAllText(file);
                    //size = text.Length;
                    // MessageBox.Show(file.ToString());
                    //label1.Text = text;
                    //StreamReader reader = new StreamReader(file);
                    //do
                    //{
                    //    ch = (char)reader.Read();
                    //    //label2.Text = ch.ToString();
                    //    if (Convert.ToInt32(ch) == 34)
                    //    {
                    //        Console.Write(@";");
                    //    }
                    //    Tchar++;
                    //} while (!reader.EndOfStream);

                    const string MatchEmailPattern =
      @"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
      + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
        + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
      + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
                    Regex rx = new Regex(MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    // Find matches.
                    MatchCollection matches = rx.Matches(text);
                    // Report the number of matches found.
                    int noOfMatches = matches.Count;
                    MessageBox.Show("E-mail Found ="+noOfMatches);
                    // Report on each match.
                    FileStream fs1 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                    StreamWriter writer = new StreamWriter(fs1);
                    foreach (Match match in matches)
                    {
                        string s = match.ToString();
                        writer.WriteLine(match.ToString() + ",");
                    }
                    writer.Close();

                    string sourceFilePath = path;
                    string destinationFilePath = path;
                    var readLines = File.ReadAllLines(sourceFilePath, Encoding.Default);
                    File.WriteAllLines(destinationFilePath, readLines.Distinct().ToArray(), Encoding.Default);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("" + ex);
                }
            }
            MessageBox.Show("Successfully Extract E-mail"); // <-- Shows file size in debugging mode.
            //<-- For debugging use.
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //        StreamWriter writer =
            //             new StreamWriter("D:\\j.txt", true);

            //            writer.WriteLine("Important data line 1");

            //        // Append line to the file.
            //StreamWriter writer1 =
            //            new StreamWriter("D:\\j.txt", true);

            //            writer1.WriteLine("Line 2");



        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            string path = Application.StartupPath + @"\\E-mail-Excel.txt";
            TextWriter tw = new StreamWriter(path);
            tw.Close();
            DialogResult result = openFileDialog2.ShowDialog();
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;

            string str;
            int rCnt = 0;
            int cCnt = 0;

            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog2.FileName;
                xlApp = new Excel.Application();
                xlWorkBook = xlApp.Workbooks.Open(file, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                range = xlWorkSheet.UsedRange;

                FileStream fs1 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter writer = new StreamWriter(fs1);

                for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
                {
                    for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                    {
                        const string MatchEmailPattern =
         @"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
         + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
           + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
         + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
                        str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                        if (str != null)
                        {
                            Regex rx = new Regex(MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                            // Find matches.
                            MatchCollection matches = rx.Matches(str);
                            //int noOfMatches = matches.Count;
                            //MessageBox.Show("E-mail Found In " + cCnt +"Row" + noOfMatches);
                            foreach (Match match in matches)
                            {
                                //MessageBox.Show(match.ToString());
                                writer.WriteLine(match.ToString() + ",");
                            }
                        }
                    }

                }

                writer.Close();
                string sourceFilePath = path;
                string destinationFilePath = path;
                var readLines = File.ReadAllLines(sourceFilePath, Encoding.Default);
                File.WriteAllLines(destinationFilePath, readLines.Distinct().ToArray(), Encoding.Default);
                xlWorkBook.Close(true, null, null);
                xlApp.Quit();

                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);
                MessageBox.Show("Successfully Extract E-mail");
            }
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        private void btnWord_Click(object sender, EventArgs e)
        {
            string path = Application.StartupPath + @"\\E-mail-Word.txt";
            TextWriter tw = new StreamWriter(path);
            tw.Close();
            DialogResult result = openFileDialog3.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog3.FileName;
                try
                {
                    
                    // Open a doc file.
                    word.Document document;
                    word.Application application;
                    application = new word.Application();
                    document = application.Documents.Open(file);
                    
                    // Loop through all words in the document.
                    int count = document.Words.Count;
                    for (int i = 1; i <= count; i++)
                    {
                        // Write the word.
                        string str = document.Words[i].Text;
                    
                    }
                    document.ActiveWindow.Selection.WholeStory();

                    document.ActiveWindow.Selection.Copy();

                    IDataObject data = Clipboard.GetDataObject();

                    string text = data.GetData(DataFormats.Text).ToString();

                    const string MatchEmailPattern =
     @"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
       + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
     + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
                    Regex rx = new Regex(MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    // Find matches.
                    MatchCollection matches = rx.Matches(text);
                    // Report the number of matches found.
                    int noOfMatches = matches.Count;
                    MessageBox.Show("E-mail Found =" + noOfMatches);
                    // Report on each match.
                    FileStream fs1 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                    StreamWriter writer = new StreamWriter(fs1);
                    foreach (Match match in matches)
                    {
                        string s = match.ToString();
                        writer.WriteLine(match.ToString() + ",");
                    }
                    writer.Close();

                    string sourceFilePath = path;
                    string destinationFilePath = path;
                    var readLines = File.ReadAllLines(sourceFilePath, Encoding.Default);
                    File.WriteAllLines(destinationFilePath, readLines.Distinct().ToArray(), Encoding.Default);
                    document.Close();
                    application.Quit();
                  
                }
                catch (Exception ex)
                {
                    MessageBox.Show("" + ex);
                }
            }
            // Close word.
            MessageBox.Show("Successfully Extract E-mail");
           

        }
    }
}
Posted
Updated 17-Dec-15 19:27pm
v9
Comments
Garth J Lancaster 29-Nov-15 2:22am    
Was there a question there ? what is supposed to do that it doesnt ? or what does it do that it shoudnt ? - please update your question to explain what you're looking for help on - we cant guess

This is not a question at all. Nevertheless, one of the available open-source libraries which can help you with nearly all of your "e-mail extraction" problems is OpenPop.NET: http://sourceforge.net/projects/hpop[^].

Don't be confused: it may seem that the library is about POP3 protocol, this is not so. Yes, client-side POP3 protocol implementation is available, but this is not the most important part of it. The project can be used as a detailed references on all kinds of standards/RFCs on MIME types, formats, everything which is needed for comprehensive parsing of an e-mail message.

—SA
 
Share this answer
 
What is the Questions actually? There are many open source email extractor available at web you can use it easily.
Emails Extractor[^]

http://emailgrabber.codeplex.com/[^]

and many more...
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900