Beispiel #1
0
        internal void ParseMessageSection(String bound, String[] lines, ref int i)
        {
            for (; i < lines.Length; i++)
            {
                lines[i] = lines[i].TrimStart('\t');

                if (lines[i] == "--" + bound + "--")
                {
                    return;                                  //its all over for this section
                }
                if (lines[i] == "--" + bound)
                {
                    //beginning of the section, start looking for headers
                    String currentType  = "text/plain";
                    String newBound     = "";
                    String filename     = "";
                    bool   isAttachment = false;

                    while (lines[i] != "")
                    {
                        var currentLine = lines[i];

                        while (lines[i + 1].StartsWith("\t") || lines[i + 1].StartsWith(" "))
                        {
                            currentLine = currentLine.Trim() + " " + lines[++i].Trim();
                        }

                        if (currentLine.StartsWith("Content-Type") || currentLine.StartsWith("boundary"))
                        {
                            String[] parts = currentLine.Split(':');
                            String[] bits  = parts[1].Split(';');
                            currentType = bits[0].Trim();

                            for (int x = 0; x < bits.Length; x++)
                            {
                                bits[x] = bits[x].Trim();
                                if (bits[x].StartsWith("boundary=\""))
                                {
                                    newBound = bits[x].Substring(10, bits[x].Length - 11);
                                }
                                else if (bits[x].StartsWith("boundary"))
                                {
                                    newBound = bits[x].Substring(9, bits[x].Length - 9);
                                }
                            }
                        }
                        else if (currentLine.StartsWith("Content-Disposition"))
                        {
                            String[] m_parts = lines[i].Split(':');
                            String[] m_bits  = m_parts[1].Split(';');
                            isAttachment = m_bits[0].Trim() == "attachment" ? true : false;

                            for (int x = 0; x < m_bits.Length; x++)
                            {
                                m_bits[x] = m_bits[x].Trim();
                                if (m_bits[x].StartsWith("filename=\""))
                                {
                                    filename = m_bits[x].Substring(10, m_bits[x].Length - 11);
                                }
                                else if (m_bits[x].StartsWith("filename="))
                                {
                                    filename = m_bits[x].Substring(9, m_bits[x].Length - 9);
                                }
                            }
                        }

                        i++;
                    }

                    if (newBound != "")
                    {
                        //check for new section
                        ParseMessageSection(newBound, lines, ref i);
                    }

                    if (MimeTypeHandler == null || MimeTypeHandler(currentType, lines, ref i, this) == false)
                    {
                        StringBuilder m_bld = new StringBuilder();

                        for (; i < lines.Length; i++)
                        {
                            if (lines[i] == "--" + bound)
                            {
                                break;
                            }
                            if (lines[i] == "--" + bound + "--")
                            {
                                break;
                            }
                            if (isAttachment)
                            {
                                m_bld.Append(lines[i]);
                            }
                            else
                            {
                                m_bld.AppendLine(lines[i]);
                            }
                        }

                        i--;
                        if (isAttachment)
                        {
                            var attachment = new Pop3Attachment
                            {
                                Name = filename,
                                Type = currentType,
                                Data = Convert.FromBase64String(m_bld.ToString())
                            };

                            //add to attachment list
                            Attachments.Add(attachment);
                        }
                        else if (currentType == "text/plain")
                        {
                            this.BodyText = m_bld.ToString();
                        }
                        else if (currentType == "text/html")
                        {
                            this.BodyHtml = m_bld.ToString();
                        }
                    }
                }
            }
        }
        internal void ParseMessageSection(String bound, String[] lines, ref int i)
        {
            for (; i < lines.Length; i++)
            {
                lines[i] = lines[i].TrimStart('\t');

                if (lines[i] == "--" + bound + "--") return; //its all over for this section

                if (lines[i] == "--" + bound)
                {
                    //beginning of the section, start looking for headers
                    String currentType = "text/plain";
                    String newBound = "";
                    String filename = "";
                    bool isAttachment = false;

                    while (lines[i] != "")
                    {
                        var currentLine = lines[i];

                        while (lines[i + 1].StartsWith("\t") || lines[i + 1].StartsWith(" "))
                        {
                            currentLine = currentLine.Trim() + " " + lines[++i].Trim();
                        }

                        if (currentLine.StartsWith("Content-Type") || currentLine.StartsWith("boundary"))
                        {
                            String[] parts = currentLine.Split(':');
                            String[] bits = parts[1].Split(';');
                            currentType = bits[0].Trim();

                            for (int x = 0; x < bits.Length; x++)
                            {
                                bits[x] = bits[x].Trim();
                                if (bits[x].StartsWith("boundary=\""))
                                {
                                    newBound = bits[x].Substring(10, bits[x].Length - 11);
                                }
                                else if (bits[x].StartsWith("boundary"))
                                {
                                    newBound = bits[x].Substring(9, bits[x].Length - 9);
                                }
                            }
                        }
                        else if (currentLine.StartsWith("Content-Disposition"))
                        {
                            String[] m_parts = lines[i].Split(':');
                            String[] m_bits = m_parts[1].Split(';');
                            isAttachment = m_bits[0].Trim() == "attachment" ? true : false;

                            for (int x = 0; x < m_bits.Length; x++)
                            {
                                m_bits[x] = m_bits[x].Trim();
                                if (m_bits[x].StartsWith("filename=\""))
                                {
                                    filename = m_bits[x].Substring(10, m_bits[x].Length - 11);
                                }
                                else if (m_bits[x].StartsWith("filename="))
                                {
                                    filename = m_bits[x].Substring(9, m_bits[x].Length - 9);
                                }
                            }
                        }

                        i++;
                    }

                    if (newBound != "")
                    {
                        //check for new section
                        ParseMessageSection(newBound, lines, ref i);
                    }

                    if (MimeTypeHandler == null || MimeTypeHandler(currentType, lines, ref i, this) == false)
                    {
                        StringBuilder m_bld = new StringBuilder();

                        for (; i < lines.Length; i++)
                        {
                            if (lines[i] == "--" + bound) break;
                            if (lines[i] == "--" + bound + "--") break;
                            if (isAttachment)
                            {
                                m_bld.Append(lines[i]);
                            }
                            else
                            {
                                m_bld.AppendLine(lines[i]);
                            }
                        }

                        i--;
                        if (isAttachment)
                        {
                            var attachment = new Pop3Attachment
                            {
                                Name = filename,
                                Type = currentType,
                                Data = Convert.FromBase64String(m_bld.ToString())
                            };

                            //add to attachment list
                            Attachments.Add(attachment);
                        }
                        else if (currentType == "text/plain")
                        {
                            this.BodyText = m_bld.ToString();
                        }
                        else if (currentType == "text/html")
                        {
                            this.BodyHtml = m_bld.ToString();
                        }
                    }

                }
            }
        }
    internal void ParseMessageSection(String bound, String[] lines, ref int i)
    {
      for (; i < lines.Length; i++)
      {
        if (lines[i] == "--" + bound + "--") return; //its all over for this section

        if (lines[i] == "--" + bound)
        {
          //beginning of the section, start looking for headers
          String m_current_type = "text/plain";
          String m_new_bound = "";
          String m_filename = "";
          bool is_attachment = false;

          while (lines[i] != "")
          {
            if (lines[i].StartsWith("Content-Type"))
            {
              String[] m_parts = lines[i].Split(':');
              String[] m_bits = m_parts[1].Split(';');
              m_current_type = m_bits[0].Trim();

              for (int x = 0; x < m_bits.Length; x++)
              {
                m_bits[x] = m_bits[x].Trim();
                if (m_bits[x].StartsWith("boundary=\""))
                {
                  m_new_bound = m_bits[x].Substring(10, m_bits[x].Length - 11);
                }
                else if (m_bits[x].StartsWith("boundary"))
                {
                  m_new_bound = m_bits[x].Substring(9, m_bits[x].Length - 9);
                }
              }
            }
            else if (lines[i].StartsWith("Content-Disposition"))
            {
              String[] m_parts = lines[i].Split(':');
              String[] m_bits = m_parts[1].Split(';');
              is_attachment = m_bits[0].Trim() == "attachment" ? true : false;

              for (int x = 0; x < m_bits.Length; x++)
              {
                m_bits[x] = m_bits[x].Trim();
                if (m_bits[x].StartsWith("filename=\""))
                {
                  m_filename = m_bits[x].Substring(10, m_bits[x].Length - 11);
                }
                else if (m_bits[x].StartsWith("filename="))
                {
                  m_filename = m_bits[x].Substring(9, m_bits[x].Length - 9);
                }
              }
            }

            i++;
          }

          if (m_new_bound != "")
          {
            //check for new section
            ParseMessageSection(m_new_bound, lines, ref i);
          }

          if (MimeTypeHandler == null || MimeTypeHandler(m_current_type, lines, ref i, this) == false)
          {
            StringBuilder m_bld = new StringBuilder();

            for (; i < lines.Length; i++)
            {
              if (lines[i] == "--" + bound) break;
              if (lines[i] == "--" + bound + "--") break;
              if (is_attachment)
              {
                m_bld.Append(lines[i]);
              }
              else
              {
                m_bld.AppendLine(lines[i]);
              }
            }

            i--;
            if (is_attachment)
            {
              Pop3Attachment m_attach = new Pop3Attachment();
              m_attach.Name = m_filename;
              m_attach.Type = m_current_type;
              m_attach.Data = Convert.FromBase64String(m_bld.ToString());

              //add to attachment list
              Attachments.Add(m_attach);
            } 
            else if (m_current_type == "text/plain")
            {
              this.BodyText = m_bld.ToString();
            }
            else if (m_current_type == "text/html")
            {
              this.BodyHtml = m_bld.ToString();
            }
          }

        }
      }
    }