Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / XML

Understanding the Insides of the IMAP Mail Protocol: Part 3

Rate me:
Please Sign up or sign in to vote.
4.89/5 (30 votes)
4 Oct 2013MIT5 min read 114.9K   3.4K   80  
This article describes the receiving mail process in IMAP for beginners of the mail protocol.
The purpose of this article is to explore the inside of the IMAP protocol and show you how to implement it with C#.
using System;
using System.Collections.Generic;
using System.Text;
using HigLabo.Net.Mail;

namespace HigLabo.Net.Internal
{
	/// <summary>
    /// Represent context of request and response process and provide data about context.
    /// </summary>
    internal class SmtpDataReceiveContext : DataReceiveContext
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="encoding"></param>
		internal SmtpDataReceiveContext(Encoding encoding):
			base(encoding)
		{
		}
		/// <summary>
		/// Read buffer data to Data property and initialize buffer.
		/// If response has next data,return true.
		/// </summary>
		/// <param name="size"></param>
		/// <returns>If response has next data,return true</returns>
		public override Boolean ReadBuffer(Int32 size)
		{
			Int64? NewLineStartIndex = null;
			Boolean isLastLine = false;
            Byte[] bb = this.Buffer;
            Byte[] lastBytes = null;

			for (int i = 0; i < size; i++)
			{
				this.Stream.WriteByte(bb[i]);
				bb[i] = 0;
				//Check the response stream has next line or not.
				if (this.Stream.Length == 4)
				{
                    lastBytes = this.GetLastByte(1);
					if (lastBytes[0] == AsciiCharCode.Space.GetNumber())
					{
						isLastLine = true;
					}
				}
				else if (this.Stream.Length > 4)
				{
                    lastBytes = this.GetLastByte(2);
                    if (lastBytes[0] == AsciiCharCode.CarriageReturn.GetNumber() &&
                        lastBytes[1] == AsciiCharCode.LineFeed.GetNumber())
                    {
                        isLastLine = true;
                    }
					NewLineStartIndex = this.Stream.Length + 1;
				}
				if (NewLineStartIndex.HasValue == true &&
					NewLineStartIndex.Value + 3 == this.Stream.Length)
				{
                    lastBytes = this.GetLastByte(1);
                    //Space is indicate last line.
					if (lastBytes[0] == AsciiCharCode.Space.GetNumber())
					{
						isLastLine = true;
					}
				}
			}

			if (isLastLine == true)
			{
				if (size < bb.Length)
				{
					return false;
				}
				//\r\n
                if (this.Stream.Length > 1)
                {
                    lastBytes = this.GetLastByte(2);
                    if (lastBytes[0] == AsciiCharCode.CarriageReturn.GetNumber() &&
                        lastBytes[1] == AsciiCharCode.LineFeed.GetNumber())
                    {
                        return false;
                    }
                }
			}
			return 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


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

Comments and Discussions