In this article, you will learn how to create a spam filter on your mail application. You will also see how to filter your mail based on whether the mail tells about a particular topic or not.
- SpamFilterSample.zip
- SpamFilterSample
- HigLabo.Mail.SampleApplication
- HigLabo.Mail
- Async
- bin
- Debug
- Release
- HigLabo.Mail.dll
- HigLabo.Net.dll
- Newtonsoft.Json.dll
- Common
- HigLabo.Mail.csproj
- HigLabo.Mail.csproj.user
- HigLabo.Mail.csproj.vspscc
- Imap
- obj
- Pop3
- Properties
- Smtp
- HigLabo.Net
- bin
- Debug
- de
- es
- fr
- it
- ja
- ko
- ru
- zh-Hans
- zh-Hant
- Release
- de
- System.Xml.Linq.resources.dll
- es
- System.Xml.Linq.resources.dll
- fr
- System.Xml.Linq.resources.dll
- HigLabo.Net.dll
- HigLabo.Net.Silverlight.dll
- HigLabo.Net.WindowsPhone7.dll
- it
- System.Xml.Linq.resources.dll
- ja
- System.Xml.Linq.resources.dll
- ko
- System.Xml.Linq.resources.dll
- Newtonsoft.Json.dll
- Newtonsoft.Json.Silverlight.dll
- Newtonsoft.Json.WindowsPhone7.dll
- ru
- System.Xml.Linq.resources.dll
- System.Xml.Linq.dll
- zh-Hans
- System.Xml.Linq.resources.dll
- zh-Hant
- System.Xml.Linq.resources.dll
- Core
- Extensions
- HigLabo.Net.csproj
- HigLabo.Net.csproj.user
- HigLabo.Net.csproj.vspscc
- HigLabo.Net.Silverlight.csproj
- HigLabo.Net.Silverlight.csproj.user
- HigLabo.Net.Silverlight.csproj.vspscc
- HigLabo.Net.WindowsPhone7.csproj
- HigLabo.Net.WindowsPhone7.csproj.user
- HigLabo.Net.WindowsPhone7.csproj.vspscc
- Http
- OAuth
- obj
- Properties
- Socket
- Reference
- BayesianFilter.dll
- bin
- Debug
- Reference.dll
- Release
- HtmlAgilityPack.dll
- obj
- Debug
- TempPE
- Properties
- Reference.csproj
- Reference.csproj.user
- SpamFilterSample
- SpamFilterSample-noexe.zip
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using HigLabo.Net;
using HigLabo.Net.Mail;
using HigLabo.Net.Pop3;
using HtmlAgilityPack;
using System.Web;
namespace SpamFilterSample
{
class Program
{
private static String UserName = "";
private static String Password = "";
static void Main(string[] args)
{
CreateSportDataFromBbc();
}
private static void FilterMail()
{
MailMessage mg = null;
List<MailMessage> spamList = new List<MailMessage>();
MailSpamFilter f = new MailSpamFilter();
f.LoadData();
using (Pop3Client cl = new Pop3Client("pop.gmail.com", 995, UserName, Password))
{
cl.Ssl = true;
cl.AuthenticateMode = Pop3AuthenticateMode.Auto;
var bl = cl.Authenticate();
if (bl == true)
{
var l = cl.ExecuteList();
for (int i = 0; i < l.Count && i < 100; i++)
{
mg = cl.GetMessage(l[i].MailIndex);
if (f.Test(mg.BodyText) == MailType.Spam)
{
spamList.Add(mg);
}
}
}
}
Console.WriteLine("Show spam subject list");
Console.WriteLine("-----------------------------------");
for (int i = 0; i < spamList.Count; i++)
{
Console.WriteLine(spamList[i].Subject);
}
Console.WriteLine("-----------------------------------");
Console.WriteLine("Exit press key");
Console.ReadLine();
}
private static void CreateSpamDataFromMailbox()
{
Console.WriteLine("Update spam data?press y");
if (Console.ReadLine() != "y") { return; }
MailMessage mg = null;
List<MailMessage> spamList = new List<MailMessage>();
StringBuilder sb = new StringBuilder(1024 * 32);
using (Pop3Client cl = new Pop3Client("pop.gmail.com", 995, "", ""))
{
cl.Ssl = true;
cl.AuthenticateMode = Pop3AuthenticateMode.Auto;
var bl = cl.Authenticate();
if (bl == true)
{
var l = cl.ExecuteList();
for (int i = 0; i < l.Count && i < 100; i++)
{
mg = cl.GetMessage(l[i].MailIndex);
sb.AppendLine(mg.BodyText);
sb.AppendLine();
}
}
}
String fileName = "Spam.txt";
if (File.Exists(fileName) == true)
{
File.Delete(fileName);
}
File.WriteAllText(fileName, sb.ToString());
}
private static void CreateSportDataFromBbc()
{
Console.WriteLine("Update SPORT data?press y");
if (Console.ReadLine() != "y") { return; }
HttpClient cl = new HttpClient();
HttpRequestCommand cm = new HttpRequestCommand("http://www.bbc.co.uk/sport/0/");
cm.MethodName = HttpMethodName.Get;
String htmlText = cl.GetBodyText(cm);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlText);
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(@"//div[@class=""type-a-headline-list-1""]//li//a[@data-published]");
List<String> urlList = new List<string>();
foreach (HtmlNode node in nodes)
{
urlList.Add(node.Attributes["href"].Value);
}
StringBuilder sb = new StringBuilder(1024 * 32);
for (int i = 0; i < urlList.Count; i++)
{
sb.AppendLine(GetArticleText("http://www.bbc.co.uk" + urlList[i]));
}
String fileName = "Sport.txt";
if (File.Exists(fileName) == true)
{
File.Delete(fileName);
}
File.WriteAllText(fileName, sb.ToString());
Console.WriteLine("Sport.txt is created at " + Environment.CurrentDirectory);
Console.ReadLine();
}
private static String GetArticleText(String url)
{
StringBuilder sb = new StringBuilder(8096);
HttpClient cl = new HttpClient();
HttpRequestCommand cm = new HttpRequestCommand(url);
cm.MethodName = HttpMethodName.Get;
String htmlText = cl.GetBodyText(cm);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlText);
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(@"//div[@class=""story-body""]//div[@class=""article""]//p");
if (nodes != null)
{
foreach (HtmlNode node in nodes)
{
sb.AppendLine(HttpUtility.HtmlDecode(node.InnerText));
}
}
return sb.ToString();
}
}
}
|
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.