Hi, I am trying to create a program to read through IIS logs and count the unique "FROM" address, and count how many emails that address has sent. I have done a horrible version of it, based on knowing the emails in the file already, but I am trying to polish this up now but getting reading the file for address, and then matching. on that. I can get ALL email address, but I just need to get the "from" one, and then match them to "0 MAIL - +FROM:<(+ email address)>" .
Can someone please advise on how I can refine my program to a) search for all distinct FROM address', and then count home may times those addresses appear in the file?
Thank you
Can someone please advise on how I can refine my program to a) search for all distinct FROM address', and then count home may times those addresses appear in the file?
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;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace IISemailCount
{
public partial class Form1 : Form
{
string fname = "", EoE = "25 QUIT - - 0 0 4 0", DOM1 = "0 MAIL - +FROM:<[EMAIL]address1@domain.com[/EMAIL]>", DOM2 = "0 MAIL - +FROM:<[EMAIL]address2@domain.com[/EMAIL]>",
DOM2 = "0 MAIL - +FROM:<[EMAIL]somethingelse@anotherdomain.co.uk[/EMAIL]>",etc;
private void button2_Click_1(object sender, EventArgs e)
{
// read data
string input = File.ReadAllText(fname);
//Console.Write(input);
// email address pattern
const string Pattern =
@"(([\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})";
// set up regex instance with options
Regex emailPattern = new Regex(Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
// perform match
MatchCollection emailMatches = emailPattern.Matches(input);
// set up our string builder
StringBuilder sb = new StringBuilder();
// build the list
mainWindow.AppendText("---EXTRACTED EMAIL ADDRESSES---\n\n");
foreach (Match emailMatch in emailMatches)
{
// add address to builder
sb.AppendLine(emailMatch.Value);
// display to console
mainWindow.AppendText("\n\n"+ emailMatch.Value);
}
}
private void button1_Click_1(object sender, EventArgs e)
{
if (searchParam.Text != "")
{
if (fname == "")
{
MessageBox.Show("No file selected.\nClick the LOAD button to select a file first.");
}
else
{
foreach (string line in File.ReadLines(fname))
{
if (line.Contains(searchParam.Text.ToString()))
{
mainWindow.AppendText(line + Environment.NewLine);
}
}
}
}
else { MessageBox.Show("Search box empty"); }
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int eoe =0, runningTotal = 0, totalcounter = 0, address1Counter = 0, address2Counter = 0, address3Counter = 0, etc;
if (fname == "")
{
MessageBox.Show("No file selected.\nClick the LOAD button to select a file first.");
}
else
{
foreach (string line in File.ReadLines(fname))
{
if (line.Contains(eoe))
{
totalcounter++;
}
if (line.Contains(DOM1))
{
address1Counter++;
}
else
if (line.Contains(DOM2))
{
address2Counter++;
}
else
if (line.Contains(DO3))
{
address3Counter++;
}
etc
}
//label3.Text = runningTotal + " e-Mails counted";
if (address1Counter > 0) { mainWindow.AppendText(address1Counter + " address1 address." + Environment.NewLine); runningTotal = runningTotal + address1Counter; }
if (address2Counter > 0) { mainWindow.AppendText(address2Counter + " address2 address" + Environment.NewLine); runningTotal = runningTotal + address2Counter; }
etc
}
label3.Text = runningTotal + " e-Mails counted";
}
private void button2_Click(object sender, EventArgs e)
{
if (mainWindow.TextLength == 0)
{
MessageBox.Show("No content to copy");
}
else
{
StringBuilder sb = new StringBuilder();
foreach (string line in mainWindow.Lines)
sb.AppendLine(line);
sb.AppendLine(label3.Text);
Clipboard.SetText(sb.ToString());
MessageBox.Show("Main windows content copied to clipboard");
}
}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter =
"log files (*.log)|*.log|All files (*.*)|*.*";
dialog.InitialDirectory = "C:\\";
dialog.Title = "Select a log file";
if (dialog.ShowDialog() == DialogResult.OK)
{
fname = dialog.FileName;
//richTextBox1.Text = System.IO.File.ReadAllText(fname);
label2.Text = Path.GetFileNameWithoutExtension(fname);
}
}
}
}
Thank you
Last edited by a moderator: