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();
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
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);
MatchCollection matches = rx.Matches(text);
int noOfMatches = matches.Count;
MessageBox.Show("E-mail Found ="+noOfMatches);
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");
}
private void Form1_Load(object sender, EventArgs e)
{
}
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)
{
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);
MatchCollection matches = rx.Matches(str);
foreach (Match match in matches)
{
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)
{
string file = openFileDialog3.FileName;
try
{
word.Document document;
word.Application application;
application = new word.Application();
document = application.Documents.Open(file);
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
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);
MatchCollection matches = rx.Matches(text);
int noOfMatches = matches.Count;
MessageBox.Show("E-mail Found =" + noOfMatches);
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);
}
}
MessageBox.Show("Successfully Extract E-mail");
}
}
}