/// <summary> /// Check if an attachment is pending before stop decoding /// </summary> /// <returns> /// If the method add an attachment the method return 1. Otherwise it return 0. ///</returns> public int CheckAttachmentEnd() { if (m_last_attachment != null) { m_last_attachment.CheckAttachmentEnd(); if (m_last_attachment.Headers.Count > 0) { m_attachments.Add(m_last_attachment); return(1); } } if (m_bodyBuilder != null) { m_body = m_bodyBuilder.ToString(); } if (m_headerBuilder != null) { m_header = m_headerBuilder.ToString(); } m_bodyBuilder = null; m_headerBuilder = null; return(0); }
/// <summary> /// Add a message line to the decoder /// </summary> /// <param name="line">A string containing a message line</param> public void AddLine(string line) { m_lines_treated++; // one line more if (m_state == decoder_state.header) { if (line != "\r\n") { if (m_headerBuilder == null) { m_headerBuilder = new StringBuilder(); } m_headerBuilder.Append(line); int r = line.IndexOf(":"); if (r != -1 && line[0] != ' ' && line[0] != '\t') { string field_name = line.Substring(0, r); string field_value = line.Substring(r + 1); AddHeader(field_name, field_value); m_last_field = field_name; } else if (m_last_field != null) { m_headers[m_last_field] += line; } } else { // decode header m_header = m_headerBuilder.ToString(); m_headerBuilder = null; m_boundary = HeaderBoundary(); m_multipart = IsMultipart(); m_state = decoder_state.body; m_bodyBuilder = new StringBuilder(); } } else if (m_state == decoder_state.body) { if (line != "\r\n") { if (line.IndexOf(m_boundary) != -1 && m_multipart) { // end last attachment if (m_last_attachment != null) { m_last_attachment.CheckAttachmentEnd(); if (m_last_attachment.Body.Length > 0 || m_last_attachment.Attachments.Count > 0 || m_last_attachment.Header.Length > 0) { m_attachments.Add(m_last_attachment); m_last_attachment = null; } } // start a new attachment m_state = decoder_state.attachment; m_body = m_bodyBuilder.ToString(); m_bodyBuilder.Length = 0; m_bodyBuilder = null; m_last_attachment = new MimeAttachment(); m_last_attachment.AddLine(line); } else { m_bodyBuilder.Append(line); // append the line to the message body } } } else if (m_state == decoder_state.attachment) { if (m_last_attachment != null) { if (line.IndexOf(m_boundary) != -1) { // attachement end m_last_attachment.CheckAttachmentEnd(); if (m_last_attachment.Header.Length > 0 || m_last_attachment.Body.Length > 0 || m_last_attachment.Attachments.Count > 0) { m_attachments.Add(m_last_attachment); } //m_attachments.Add (m_last_attachment); m_last_attachment = new MimeAttachment(); m_last_attachment.AddLine(line); } else { m_last_attachment.AddLine(line); // append the line to current attachment } } else { if (line.IndexOf(m_boundary) != -1) { m_last_attachment = new MimeAttachment(); m_last_attachment.AddLine(line); } } } }