我是C#和Outlook加载项的新手,更不用说贝叶斯垃圾邮件过滤了。我正在创建一个Outlook加载项,以检测垃圾邮件或网络钓鱼邮件以进行家庭作业,而我的工作时间不到一周。我发现了一些开源类,但不确定如何实现或集成。将不胜感激任何形式的帮助。谢谢!
以下链接是在CodePlex上找到源的地方,所有功劳归相关创建者所有:https://www.codeproject.com/Articles/23472/A-Naive-Bayesian-Spam-Filter-for-C,我想学习如何同时使用Corpus.cs和SpamFilter.cs,以下是我的起点基于我发现的其他开源和教程的插件上的内容
namespace MajorProject
{
public partial class ThisAddIn
{
Outlook.NameSpace outlookNameSpace;
Outlook.MAPIFolder inbox;
Outlook.Items items;
Outlook.Explorer currentExplorer = null;
//private SpamFilter _filter;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.MAPIFolder inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
//inbox.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
currentExplorer = this.Application.ActiveExplorer();
currentExplorer.SelectionChange += new Outlook
.ExplorerEvents_10_SelectionChangeEventHandler
(CurrentExplorer_Event);
}
private void CurrentExplorer_Event()
{
Outlook.MAPIFolder selectedFolder =
this.Application.ActiveExplorer().CurrentFolder;
try
{
if (this.Application.ActiveExplorer().Selection.Count > 0)
{
Object selected = this.Application.ActiveExplorer().Selection[1];
if (selected is Outlook.MailItem)
{
Outlook.MailItem mailItem =
(selected as Outlook.MailItem);
Parser.ParseLinks (mailItem.HTMLBody);
mailItem.Display(false);
}
}
}
catch (Exception ex)
{
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Note: Outlook no longer raises this event. If you have code that
// must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
public class Parser
{
readonly Expat.Bayesian.Corpus corpus = new Corpus();
readonly Expat.Bayesian.SpamFilter spam = new SpamFilter();
public static readonly Regex HyperlinkRegex = new Regex("http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
//http://weblogs.asp.net/farazshahkhan/regex-to-find-url-within-text-and-make-them-as-link
public static string ParseLinks(string str)
{
//parse through with the regex
MatchCollection HyperLinkmatches = HyperlinkRegex.Matches(str);
List<string> LinksList = new List<string>();
int x = 0;
foreach (Match match in HyperLinkmatches)
{
LinksList.Add(str);
str = str.Replace(match.Value, "<a target='_blank' href='" + match.Value + "'>" + match.Value + "</a>");
string rx = "<a\\s+ .*? href\\s*=\\s*(?:\"|') (?<url>.*?) (?:\"|') .*?> (?<anchorText>.*?) \\</a>";
Regex regex = new Regex(rx, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
string regexReplace = "${anchorText} [${url}]";
string result = regex.Replace(str, regexReplace);
x++;
}
if (x > 0)
{
MessageBox.Show("Link found" );
}
else
{
MessageBox.Show("Link not found");
}
return str;
}