Provides properties and methods for checking e-mails messages for spam probability and learning the filter from proven spam and non-spam messages.
For a list of all members of this type, see BayesFilter Members.
System.Object
MailBee.AntiSpam.BayesFilter
Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.
The filter uses the existing database of spam and non-spam messages to score new messages in the range of 0-100%. 0% corresponds to absolutely non-spam message, 100% corresponds to absolutely spam message.
The key point is learning the filter: telling the filter which messages are spam or non-spam. This is called training the filter.
Initially, while the database is empty, the filter has no existing messages which to compare with the message in question.
Thus, the first task is to train the filter with several hundreds of typical spam and non-spam messages you usually get. This will increase the filter efficiency from zero to the suitable value. The training should, however, continue in the future to further improve the quality of spam recognition. The larger database is, the better spam/non-spam recognition is.
The filter will operate correctly ONLY if it was trained with a good number of spam AND non-spam messages.
You can train the filter using TrainFilter method.
To score a message (determine if it's spam or not), use ScoreMessage method.
Note MailBee also supports other technologies which can be used as a sort of antispam check. They include full support of DomainKeys (signing and verification of e-mails using DomainKeys signatures) and DNX MX/Reverse DNS checks (see GetMXHosts and GetPtrData methods).
This sample trains the Bayesian filter for spam and for non-spam messages; saves resulting database and scores sample e-mails for spam probability using this database.
It's assumed the spam and non-spam samples are .EML files located in C:\AntiSpam\Spam and C:\AntiSpam\NonSpam folders respectively. The database itself (spam.dat and nonspam.dat) will be saved in C:\AntiSpam folder.
[C#] // To use the code below, import these namespaces at the top of your code. using System; using System.IO; using MailBee.Mime; using MailBee.AntiSpam; class Sample { static void Main(string[] args) { BayesFilter filter = new BayesFilter(); MailMessage msg = new MailMessage(); // Train Bayesian filter for spam messages. string[] files = Directory.GetFiles(@"C:\AntiSpam\Spam", "*.eml"); foreach (string file in files) { msg.LoadMessage(file); filter.TrainFilter(msg, true); // Mark as spam. } // Train Bayesian filter for non-spam messages. files = Directory.GetFiles(@"C:\AntiSpam\NonSpam", "*.eml"); foreach (string file in files) { msg.LoadMessage(file); filter.TrainFilter(msg, false); // Mark as non-spam. } // Save Bayesian database to disk. filter.SaveDatabase(@"C:\AntiSpam\spam.dat", @"C:\AntiSpam\nonspam.dat"); // Test our emails for spam. files = Directory.GetFiles(@"C:\AntiSpam\Emails", "*.eml"); foreach (string file in files) { msg.LoadMessage(file); Console.WriteLine("Spam probability is: {0}%", filter.ScoreMessage(msg)); } } }
[Visual Basic] ' To use the code below, import these namespaces at the top of your code. Imports System.IO Imports MailBee.Mime Imports MailBee.AntiSpam Module Sample Sub Main(ByVal args() As String) Dim filter As BayesFilter = New BayesFilter() Dim msg As MailMessage = New MailMessage() ' Train Bayesian filter for spam messages. Dim files() As String = Directory.GetFiles("C:\AntiSpam\Spam", "*.eml") For Each file As String In files msg.LoadMessage(file) filter.TrainFilter(msg, True) ' Mark as spam. Next ' Train Bayesian filter for non-spam messages. files = Directory.GetFiles("C:\AntiSpam\NonSpam", "*.eml") For Each file As String In files msg.LoadMessage(file) filter.TrainFilter(msg, False) ' Mark as non-spam. Next ' Save Bayesian database to disk. filter.SaveDatabase("C:\AntiSpam\spam.dat", "C:\AntiSpam\nonspam.dat") ' Test our emails for spam. files = Directory.GetFiles("C:\AntiSpam\Emails", "*.eml") For Each file As String In files msg.LoadMessage(file) Console.WriteLine("Spam probability is: {0}%", filter.ScoreMessage(msg)) Next End Sub End Module
Namespace: MailBee.AntiSpam
Assembly: MailBee.NET (in MailBee.NET.dll)
BayesFilter Members | MailBee.AntiSpam Namespace