Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Understanding the Insides of the SMTP Mail Protocol: Part 1

, 9 Dec 2012 MIT
This article describes the mail sending process using the SMTP mail protocol.
HigLaboSampleSource-noexe.zip
Source
HigLabo.Mail.SampleApplication
HigLabo.Mail.SampleApplication.suo
HigLabo.Mail.SampleApplication.v11.suo
Properties
HigLabo.Mail
Async
Common
HigLabo.Mail.csproj.user
HigLabo.Mail.csproj.vspscc
Imap
Command
Pop3
Command
Properties
Smtp
Command
SendMail
HigLabo.Net
Core
Extensions
HigLabo.Net.csproj.user
HigLabo.Net.csproj.vspscc
HigLabo.Net.Silverlight.csproj.user
HigLabo.Net.Silverlight.csproj.vspscc
HigLabo.Net.WindowsPhone7.csproj.user
HigLabo.Net.WindowsPhone7.csproj.vspscc
Http
OAuth
Properties
Socket
HigLaboSampleSource.zip
bin
Release
HigLabo.Mail.dll
HigLabo.Mail.SampleApplication.exe
HigLabo.Net.dll
Newtonsoft.Json.dll
HigLabo.Mail.SampleApplication.suo
HigLabo.Mail.SampleApplication.v11.suo
bin
Release
HigLabo.Mail.dll
HigLabo.Net.dll
Newtonsoft.Json.dll
HigLabo.Mail.csproj.user
HigLabo.Mail.csproj.vspscc
bin
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
HigLabo.Net.csproj.user
HigLabo.Net.csproj.vspscc
HigLabo.Net.Silverlight.csproj.user
HigLabo.Net.Silverlight.csproj.vspscc
HigLabo.Net.WindowsPhone7.csproj.user
HigLabo.Net.WindowsPhone7.csproj.vspscc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace HigLabo.Net
{
    /// <summary>
    /// 
    /// </summary>
#if !SILVERLIGHT
    [Serializable]
#endif
    public class HttpRequestCommand
    {
        private WebHeaderCollection _Headers = new WebHeaderCollection();
        /// <summary>
        /// 
        /// </summary>
        public String Url { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public HttpMethodName MethodName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Boolean IsSendBodyStream { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public WebHeaderCollection Headers
        {
            get { return _Headers; }
        }
        /// <summary>
        /// 
        /// </summary>
        public String ContentType { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Stream BodyStream { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public Func<String, String> UrlEncodeFunction { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        public HttpRequestCommand(String url)
        {
            this.InitializeProperty();
            this.Url = url;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="methodName"></param>
        public HttpRequestCommand(String url, HttpMethodName methodName)
        {
            this.InitializeProperty();
            this.Url = url;
            this.MethodName = methodName;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="stream"></param>
        public HttpRequestCommand(String url, Stream stream)
        {
            this.InitializeProperty();
            this.Url = url;
            this.MethodName = HttpMethodName.Post;
            this.BodyStream = stream;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="encoding"></param>
        /// <param name="values"></param>
        public HttpRequestCommand(String url, Encoding encoding, Dictionary<String, String> values)
        {
            this.InitializeProperty();
            this.Url = url;
            this.MethodName = HttpMethodName.Post;
            this.ContentType = HttpClient.ApplicationFormUrlEncoded;
            this.BodyStream = new MemoryStream(this.CreateRequestBodyData(encoding, values));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        public HttpRequestCommand(String url, Byte[] data)
        {
            this.InitializeProperty();
            this.Url = url;
            this.MethodName = HttpMethodName.Post;
            this.BodyStream = new MemoryStream(data);
        }
        private void InitializeProperty()
        {
            this.UrlEncodeFunction = HttpClient.UrlEncode;
            this.MethodName = HttpMethodName.Get;
            this.IsSendBodyStream = false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="encoding"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public Byte[] CreateRequestBodyData(Encoding encoding, Dictionary<String, String> values)
        {
            StringBuilder sb = new StringBuilder(512);
            var d = values;
            if (d == null || d.Keys.Count == 0) { return new Byte[0]; }

            foreach (var key in d.Keys)
            {
                sb.AppendFormat("{0}={1}&", key, this.UrlEncodeFunction(d[key]));
            }
            return encoding.GetBytes(sb.ToString());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="encoding"></param>
        /// <param name="values"></param>
        public void SetBodyStream(Encoding encoding, Dictionary<String, String> values)
        {
            if (String.IsNullOrEmpty(this.ContentType) == true)
            {
                this.ContentType = HttpClient.ApplicationFormUrlEncoded;
            }
            var bb = this.CreateRequestBodyData(encoding, values);
            this.BodyStream = new MemoryStream(bb);
            this.IsSendBodyStream = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public void SetBodyStream(Byte[] data)
        {
            this.BodyStream = new MemoryStream(data);
            this.IsSendBodyStream = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        public void SetBodyStream(Stream stream)
        {
            this.BodyStream = stream;
            this.IsSendBodyStream = true;
        }
    }
}

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

Share

About the Author

Higty
Web Developer
Japan Japan
I'm Working at Software Company in Tokyo.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 10 Dec 2012
Article Copyright 2012 by Higty
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid