/// <summary> /// Get the current content mapping header type for the current line /// </summary> /// <param name="line">The current message line.</param> /// <param name="boundary">The boundary unique string for the current conten.</param> /// <returns>The content mapping header type.</returns> public static Nequeo.Net.Mail.MesssageContentMappingType GetContentMappingHeaderType(string line, string boundary) { // Set the initial content mapping header typ the unknown. Nequeo.Net.Mail.MesssageContentMappingType lineType = Nequeo.Net.Mail.MesssageContentMappingType.UnKnown; // For each content mapping header type string. for (int i = 0; i < EmailMessageParse.ContentMappingHeader.Length; i++) { // Get the current content mapping header type string. string match = EmailMessageParse.ContentMappingHeader[i]; // Find a match in the current message line // with the current content mapping haeder type. if (Regex.Match(line, "^" + match + ":" + ".*$").Success) { // Cast the matching content mapping header type // with the current array of content mapping header type. // Break out of the loop. lineType = (Nequeo.Net.Mail.MesssageContentMappingType)i; break; } else { // If the current line is a start boundary unique string // for body or attachment content mapping header type. if (line.Equals("--" + boundary)) { // Set the current content mapping header type // to start of multipart boundary found, indicating that the // message is a multipart message with a starting // boundary unique string. Break out of the loop. lineType = Nequeo.Net.Mail.MesssageContentMappingType.MultipartBoundaryFound; break; } else { // If the current line is an end boundary unique string // for body or attachment conrent mapping header type. if (line.Equals("--" + boundary + "--")) { // Set the current content mapping header type // to end of multipart boundary found. all the // body or attachment data has been collected // in a multipart messsage. lineType = Nequeo.Net.Mail.MesssageContentMappingType.ComponentDone; break; } } } // If the current line length is zero. if (line.Length == 0) { // Set the content mapping header type to end // of header and break out of the loop. lineType = Nequeo.Net.Mail.MesssageContentMappingType.EndOfHeader; break; } } // Return the content mapping header type // that was found. return(lineType); }
/// <summary> /// Message body and attachment constructor. /// </summary> /// <param name="lines">The array of lines in the message.</param> /// <param name="startOfBody">The start of the body data.</param> /// <param name="multipartBoundary">The multipary boundary.</param> /// <param name="mainContentType">The main content type.</param> /// <param name="writeAttachmentToFile">Write to file.</param> /// <param name="connection">The connection object.</param> public MessageBody(string[] lines, long startOfBody, string multipartBoundary, string mainContentType, bool writeAttachmentToFile, Pop3ConnectionAdapter connection) { // Get the number of message lines. long stopOfBody = lines.Length; // If the multipart boudary is null then // only a body part exists. if (multipartBoundary == null) { // Create a new string builder. StringBuilder sbText = new StringBuilder(); // For each line in the message // append the body lines. for (long i = startOfBody; i < stopOfBody; i++) { sbText.Append(lines[i].Replace("\n", "").Replace("\r", "")); } // Add a new body to the component // list store. _component.Add(new MessageAttachment(mainContentType, sbText.ToString(), writeAttachmentToFile, connection)); } // Multipart boundary exists then a body and attachment // exists in the message. else { // Get the multipart boundary // string in the message. string boundary = multipartBoundary; // Set as first component or body // of the message to get. bool firstComponent = true; // Loop through whole of email message // get the bodt all attachments. for (long i = startOfBody; i < stopOfBody;) { bool boundaryFound = true; string contentType = null; string name = null; string filename = null; string contentTransferEncoding = null; string contentDescription = null; string contentDisposition = null; string data = null; // If the first component is to be added // this is the body of a multipart message. if (firstComponent) { boundaryFound = false; firstComponent = false; // For all the lines in the message. while (i < stopOfBody) { // Get the current line in the message. string line = lines[i].Replace("\n", "").Replace("\r", ""); // If the end of the body boundary // has been found then exit the loop // all body data has been found. if (EmailMessageParse.GetContentMappingHeaderType(line, boundary) == Nequeo.Net.Mail.MesssageContentMappingType.MultipartBoundaryFound) { boundaryFound = true; ++i; break; } else { ++i; } } } // Check to see whether multipart boundary // was not found. This will indicate that // the message data is corrupt. if (!boundaryFound) { throw new Exception("Missing multipart boundary: " + boundary); } // Initalise the end of header indicator. bool endOfHeader = false; // Read all header information. // For all other lines in the // messsage. while ((i < stopOfBody)) { // Get the current line in the message. string line = lines[i].Replace("\n", "").Replace("\r", ""); // Get the current content mapping type. Nequeo.Net.Mail.MesssageContentMappingType lineType = EmailMessageParse.GetContentMappingHeaderType(line, boundary); // Get the mapping type. switch (lineType) { case Nequeo.Net.Mail.MesssageContentMappingType.ContentType: // Mapping type is content type header. // Get the content type data. contentType = EmailMessageParse.ContentType(line); break; case Nequeo.Net.Mail.MesssageContentMappingType.ContentTransferEncoding: // Mapping type is content transfer encoding header. // Get the content transfer encoding data. contentTransferEncoding = EmailMessageParse.ContentTransferEncoding(line); break; case Nequeo.Net.Mail.MesssageContentMappingType.ContentDisposition: // Mapping type is content disposition header. // Get the content disposition data. contentDisposition = EmailMessageParse.ContentDisposition(line); break; case Nequeo.Net.Mail.MesssageContentMappingType.ContentDescription: // Mapping type is content description header. // Get the content description data. contentDescription = EmailMessageParse.ContentDescription(line); break; case Nequeo.Net.Mail.MesssageContentMappingType.EndOfHeader: // End of header has been found. endOfHeader = true; break; } // Increment to the next line after // find the content mapping type in // the prevoius line. ++i; // If end of header found then search // for the next component starting point // search for the next attachment. break // out of the loop and end the process. if (endOfHeader) { break; } else { // Read all header information. // For all other lines in the // messsage. while (i < stopOfBody) { // If more lines to read for this line. if (line.Substring(line.Length - 1, 1).Equals(";")) { // Get the current line in the message. string nextLine = lines[i].Replace("\r", "").Replace("\n", ""); // Get the current attachment header type. // interate through each attachment data. switch (EmailMessageParse.GetAttachmenHeaderType(nextLine)) { case Nequeo.Net.Mail.MesssageAttachmentType.Name: // Attachment name header type. // Get the attachment name in the message. name = EmailMessageParse.Name(nextLine); break; case Nequeo.Net.Mail.MesssageAttachmentType.Filename: // Attachment file nale header type. // Get the attachment file name in the message. filename = EmailMessageParse.Filename(nextLine); break; case Nequeo.Net.Mail.MesssageAttachmentType.EndOfHeader: // End of header has been found. endOfHeader = true; break; } // If the end of the attachment // data has not been found if (!endOfHeader) { // Assign the current line to // the next line line = nextLine; ++i; } else { // End the loop. break; } } else { // End the loop. break; } } } } // Boundary found to false. boundaryFound = false; // Create a new string builder. StringBuilder sbText = new StringBuilder(); // Initailise the emial composed indicator. bool emailComposed = false; // For each remaining line in // the message store the body // and the attachment. while (i < stopOfBody) { // Get the current line in the message. string line = lines[i].Replace("\n", ""); // If the end of the body boundary // has been found then exit the loop // all component data has been found. if (EmailMessageParse.GetContentMappingHeaderType(line, boundary) == Nequeo.Net.Mail.MesssageContentMappingType.MultipartBoundaryFound) { boundaryFound = true; ++i; break; } // If the component is done then // the email is composed, this // component has been completed // composed. else if (EmailMessageParse.GetContentMappingHeaderType(line, boundary) == Nequeo.Net.Mail.MesssageContentMappingType.ComponentDone) { emailComposed = true; break; } // Add the current line of component // data to the string builder. // Move to the next line in the message. sbText.Append(lines[i]); ++i; } // If data has been built // then this is the component data // that is body or attachment data. if (sbText.Length > 0) { data = sbText.ToString(); } // Add a new message body or attachment to the // component list store. _component.Add(new MessageAttachment(contentType, name, filename, contentTransferEncoding, contentDescription, contentDisposition, data, writeAttachmentToFile, connection)); // If all multiparts have been // composed then exit break // out of the loop. if (emailComposed) { break; } } } }