Example #1
0
        private void DetectBody()
        {
            String type = String.Empty;

            if (this.ContentType != null)
            {
                type = this.ContentType.MimeType.ToLower();
            }

            List <String> lines = new List <String>();

            Byte[] data;

            Int32 mode = 0;

            if (type.StartsWith("text") || type.StartsWith("message"))
            {
                mode = 1;
            }

            if (type.StartsWith("multipart"))
            {
                mode = 2;
            }

            switch (mode)
            {
            case 0:

                this.IsFile = true;

                foreach (String line in this.Body)
                {
                    switch (this.ContentTransferEncoding)
                    {
                    case "base64":

                        lines.Add(line);

                        break;

                    case "quoted-printable":

                        if (line.EndsWith("="))
                        {
                            lines.Add(line.Remove(line.Length - 1));
                        }
                        else
                        {
                            lines.Add(line);
                        }

                        break;
                    }
                }

                switch (this.ContentTransferEncoding)
                {
                case "7bit":

                    this.BinaryContent = Encoding.UTF7.GetBytes(String.Join("\n", lines));

                    break;

                case "8bit":

                    this.BinaryContent = Encoding.UTF8.GetBytes(String.Join("\n", lines));

                    break;

                case "base64":

                    data = Util.DecodeBase64(String.Join(String.Empty, lines));

                    this.BinaryContent = data;

                    break;

                case "quoted-printable":

                    data = Util.DecodeQuoted(String.Join(String.Empty, lines));

                    this.BinaryContent = data;

                    break;
                }

                break;

            case 1:

                this.IsText = true;

                foreach (String line in this.Body)
                {
                    switch (this.ContentTransferEncoding)
                    {
                    case "7bit":

                        lines.Add(line);

                        break;

                    case "8bit":

                        lines.Add(line);

                        break;

                    case "base64":

                        lines.Add(line);

                        break;

                    case "quoted-printable":

                        if (line.EndsWith("="))
                        {
                            lines.Add(line.Remove(line.Length - 1));
                        }
                        else
                        {
                            lines.Add(line);
                        }

                        break;

                    default:

                        lines.Add(line);

                        break;
                    }
                }

                switch (this.ContentTransferEncoding)
                {
                case "7bit":

                    this.StringContent = String.Join("\n", lines);

                    break;

                case "8bit":

                    this.StringContent = String.Join("\n", lines);

                    break;

                case "base64":

                    data = Util.DecodeBase64(String.Join(String.Empty, lines));

                    if (data.Length == 0)
                    {
                        this.StringContent = String.Empty;
                    }
                    else
                    {
                        this.StringContent = Util.DetectCharset(data, this.ContentType.Charset).GetString(data);
                    }

                    break;

                case "quoted-printable":

                    data = Util.DecodeQuoted(String.Join(String.Empty, lines));

                    if (data.Length == 0)
                    {
                        this.StringContent = String.Empty;
                    }
                    else
                    {
                        this.StringContent = Util.DetectCharset(data, this.ContentType.Charset).GetString(data).Replace("_", " ");
                    }

                    break;

                default:

                    this.StringContent = String.Join("\n", lines);

                    break;
                }

                break;

            case 2:

                this.IsMultipart = true;

                String partStart = String.Format("--{0}", this.ContentType.Boundary);

                String partClose = String.Format("--{0}--", this.ContentType.Boundary);

                foreach (String line in this.Body)
                {
                    if (line.Equals(partStart))
                    {
                        if (lines.Count != 0)
                        {
                            MimePart part = new MimePart();

                            part.RawString = String.Join("\n", lines);

                            part.Parse();

                            this.PartContents.Add(part);
                        }

                        lines.Clear();

                        continue;
                    }

                    if (line.Equals(partClose))
                    {
                        MimePart part = new MimePart();

                        part.RawString = String.Join("\n", lines);

                        part.Parse();

                        this.PartContents.Add(part);

                        break;
                    }

                    lines.Add(line);
                }

                break;
            }
        }