Poniżej przedstawiam prosty program w konsoli (można go rozbudować czy też dorobić do niego GUI) do parsowania e-maili
napisany w języku C#
using System;
using System.IO;
using System.Collections.Generic;
namespace BatchReader_01 {
public class Message {
public static int counter = 0;
public static Dictionary<string, int> dict = new Dictionary<string, int>();
public static List<Message> list = new List<Message>();
int number;
String mSubject;
String mData;
String mFrom;
public Message(ref StreamReader sr) {
list.Add(this);
++counter;
number = counter;
string strLine;
strLine = sr.ReadLine();
mData = strLine.Substring(6);
strLine = sr.ReadLine();
mFrom = strLine.Substring(6);
strLine = sr.ReadLine();
mSubject = "";
while (!strLine.StartsWith("To")) {
mSubject += strLine;
strLine = sr.ReadLine();
}
mSubject = mSubject.Substring(9);
mSubject = mSubject.Replace('t', ' ');
strLine = sr.ReadLine();
while (strLine != "------------------------------") {
strLine = sr.ReadLine();
}
if (!mSubject.StartsWith("Re: ")) {
dict.Add(mSubject.Replace(" ", ""), number);
} else {
strLine = mSubject.Substring(4).Replace(" ", "");
if (!dict.ContainsKey(strLine))
dict.Add(strLine, number);
}
}
//
public void dump() {
Console.WriteLine("Messge " + number.ToString()+":");
Console.WriteLine("t" + mSubject);
Console.WriteLine("t" + mData);
Console.WriteLine("t" + mFrom);
}
//
public static void listDump() {
foreach (Message obj in list)
obj.dump();
}
//
public static void subjectsDump() {
// Get a collection of the keys (names).
ICollection<String> c = dict.Keys;
foreach (String str in c)
Console.WriteLine("index: {0}, {1} {2}", dict[str], list[dict[str] - 1].mSubject, str);
}
}
class Program {
//-------------------------------------------------------
public static void Main(string[] args) {
try {
//List<Message> list = new List<Message>();
StreamReader sr = File.OpenText("GDAlgorithms-list Digest, Vol 28, Issue 1.MSG");
string strLine;
while (null != (strLine = sr.ReadLine())) {
if (strLine.StartsWith("Message:")) {
Message m = new Message(ref sr);
//list.Add(m);
}
}
sr.Close();
Message.subjectsDump();
Message.listDump();
} catch (FileNotFoundException e) {
Console.WriteLine(e.Message);
}
}
//-----------------------------------------------
}
}
//---------------------------------------------------------------
Projekt w VS2008 Express Edition (download)
Jak użyjesz kodu w swoim programie to napisz. Tak z ciekawości chciałbym wiedzieć 🙂
Proszę o komentarze i uwagi 🙂