Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#

How to Create a Spam Filter or Automatic Category Sort Algorithm with Your Mail Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
29 Jul 2012MIT3 min read 40K   1.2K   19  
This article describes automatic category filters in mail applications.
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.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using HigLabo.Net.Mail;

namespace HigLabo.Net.Pop3
{
    /// <summary>
    /// 
    /// </summary>
    public class UidlCommandResult
    {
        private class RegexList
        {
            public static readonly Regex MessageIndex = new Regex(@"^([0-9]+)[\s|\t]+.*$", RegexOptions.None);
            public static readonly Regex Uid = new Regex(@"^[0-9]+[\s|\t]+([\x21-\x7E]+)$", RegexOptions.None);
        }
        private Int64 _MailIndex = 0;
        private String _Uid = "";
        /// <summary>
        /// 
        /// </summary>
        public Int64 MailIndex
        {
            get { return this._MailIndex; }
        }
        /// <summary>
        /// 
        /// </summary>
        public String Uid
        {
            get { return this._Uid; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        public UidlCommandResult(String text)
        {
            if (text == null)
            { throw new ArgumentNullException("text"); }

            this._MailIndex = UidlCommandResult.GetMessageIndex(text);
            this._Uid = UidlCommandResult.GetUid(text);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mailIndex"></param>
        /// <param name="uid"></param>
        public UidlCommandResult(Int64 mailIndex, String uid)
        {
            this._MailIndex = mailIndex;
            this._Uid = uid;
        }
        /// 受信した行の文字列を解析し、メールのIndexを取得します。
        /// <summary>
        /// 受信した行の文字列を解析し、メールのIndexを取得します。
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        private static Int64 GetMessageIndex(String line)
        {
            return Int64.Parse(RegexList.MessageIndex.Replace(line.Replace("\r\n", ""), "$1"));
        }
        /// 受信した行の文字列を解析し、メールのUIDを取得します。
        /// <summary>
        /// 受信した行の文字列を解析し、メールのUIDを取得します。
        /// </summary>
        /// <param name="line"></param>
        /// <remarks>Characters in the range 0x21 to 0x7E: http://www.ietf.org/rfc/rfc1939.txt</remarks>
        /// <returns></returns>
        private static String GetUid(String line)
        {
            return RegexList.Uid.Replace(line.Replace("\r\n", ""), "$1");
        }
    }
}

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.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
CEO TinyBetter, Inc
Japan Japan
I'm a CEO of TinyBetter, Inc in Japan.

Comments and Discussions