Click here to Skip to main content
15,881,882 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.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace HigLabo.Net
{
    /// <summary>
    /// 
    /// </summary>
    public partial class HttpClient
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public HttpWebResponse GetHttpWebResponse(String url)
        {
            return this.GetHttpWebResponse(new HttpRequestCommand(url));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public HttpWebResponse GetHttpWebResponse(String url, Dictionary<String, String> values)
        {
            return this.GetHttpWebResponse(new HttpRequestCommand(url, this.RequestEncoding, values));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public HttpWebResponse GetHttpWebResponse(String url, Byte[] data)
        {
            return this.GetHttpWebResponse(new HttpRequestCommand(url, data));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public HttpWebResponse GetHttpWebResponse(String url, Stream stream)
        {
            return this.GetHttpWebResponse(new HttpRequestCommand(url, stream));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public HttpWebResponse GetHttpWebResponse(HttpRequestCommand command)
        {
            HttpWebRequest req = this.CreateRequest(command);
            StreamWriteContext scx = null;

            if (command.BodyStream == null || command.IsSendBodyStream == false)
            {
                return req.GetResponse() as HttpWebResponse;
            }
            else 
            {
                //Request body
                req.ContentLength = command.BodyStream.Length;
                if (command.BodyStream.Length > 0)
                {
                    using (var stm = req.GetRequestStream())
                    {
                        if (this.RequestBufferSize.HasValue == true)
                        {
                            scx = new StreamWriteContext(stm, this.RequestBufferSize.Value);
                        }
                        else
                        {
                            scx = new StreamWriteContext(stm);
                        }
                        scx.Uploading += (o, e) => this.OnUploading(e);
                        scx.Write(command.BodyStream);
                        stm.Close();
                    }
                }
            }
            return req.GetResponse() as HttpWebResponse;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public HttpResponse GetResponse(String url)
        {
            return this.GetResponse(new HttpRequestCommand(url));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public HttpResponse GetResponse(String url, Dictionary<String, String> values)
        {
            return this.GetResponse(new HttpRequestCommand(url, this.RequestEncoding, values));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public HttpResponse GetResponse(String url, Byte[] data)
        {
            return this.GetResponse(new HttpRequestCommand(url, data));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public HttpResponse GetResponse(String url, Stream stream)
        {
            return this.GetResponse(new HttpRequestCommand(url, stream));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public HttpResponse GetResponse(HttpRequestCommand command)
        {
            HttpWebResponse res = null;
            HttpResponse hr = null;
            try
            {
                res = this.GetHttpWebResponse(command);
                hr = new HttpResponse(res, this.ResponseEncoding);
            }
            catch (WebException ex)
            {
                res = ex.Response as HttpWebResponse;
                if (res != null)
                {
                    hr = new HttpResponse(res, this.ResponseEncoding);
                }
                else
                {
                    throw;
                }
            }
            return hr;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public String GetBodyText(String url)
        {
            return this.GetBodyText(new HttpRequestCommand(url));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public String GetBodyText(String url, Dictionary<String, String> values)
        {
            return this.GetBodyText(new HttpRequestCommand(url, this.RequestEncoding, values));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public String GetBodyText(String url, Byte[] data)
        {
            return this.GetBodyText(new HttpRequestCommand(url, data));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public String GetBodyText(String url, Stream stream)
        {
            return this.GetBodyText(new HttpRequestCommand(url, stream));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public String GetBodyText(HttpRequestCommand command)
        {
            var res = this.GetHttpWebResponse(command);
            StreamReader sr = new StreamReader(res.GetResponseStream(), this.ResponseEncoding);
            return sr.ReadToEnd();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="idKey"></param>
        /// <param name="id"></param>
        /// <param name="passwordKey"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static CookieContainer GetCookieContainer(String url, String idKey, String id, String passwordKey, String password)
        {
            return GetCookieContainer(url, DefaultEncoding, idKey, id, passwordKey, password, new Dictionary<string, string>());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="responseEncoding"></param>
        /// <param name="idKey"></param>
        /// <param name="id"></param>
        /// <param name="passwordKey"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static CookieContainer GetCookieContainer(String url, Encoding responseEncoding, String idKey, String id, String passwordKey, String password)
        {
            return GetCookieContainer(url, responseEncoding, idKey, id, passwordKey, password, new Dictionary<string, string>());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="responseEncoding"></param>
        /// <param name="idKey"></param>
        /// <param name="id"></param>
        /// <param name="passwordKey"></param>
        /// <param name="password"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static CookieContainer GetCookieContainer(String url, Encoding responseEncoding, String idKey, String id, String passwordKey, String password
            , Dictionary<String, String> values)
        {
            CookieContainer cc = new CookieContainer();
            HttpClient cl = new HttpClient();
            cl.ResponseEncoding = responseEncoding;
            cl.CookieContainer = cc;

            var cm = new HttpRequestCommand(url);
            cm.ContentType = HttpClient.ApplicationFormUrlEncoded;
            cm.MethodName = HttpMethodName.Post;
            var d = cm.Headers;
            d[idKey] = id;
            d[passwordKey] = password;
            foreach (var key in values.Keys)
            {
                d[key] = values[key];
            }
            var res = cl.GetResponse(cm);

            return cc;
        }
    } 
}

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