/// <summary> /// Parse a string into a System.Net.Mail.Message /// </summary> /// <param name="message"></param> /// <param name="id">The Unique Identifer of this message</param> /// <returns></returns> public MailMessage ParseMessage(string message, string id) { _message = new MailMessage(); _loc = PARSERLOCATION.PARSER_INHEADERS; // Start by parsing the headers try /// Here we go" { // Split the message by each line for easier parsing string[] splitter ={"\r\n"}; string[] lines = message.Split(splitter, StringSplitOptions.None); int lineCounter = 0; // Parse all of the headers ParseHeaderLines(lines, ref lineCounter); // Is this a multi part message? if (_boundary != "") { this._loc = PARSERLOCATION.PARSER_INBODY_MULTIPART_HEADER; ParseMultiPart(lines, ref lineCounter); // MultiPart Message } else { this._loc = PARSERLOCATION.PARSER_INBODY_SINGLEPART; ParsePart(lines, ref lineCounter); // Single Part Message } } catch (Exception e) { throw new ApplicationException("Could not parse message data", e); } return _message; }
private void ParsePart(string[] lines, ref int currentLine) { System.Text.StringBuilder part = new StringBuilder(); string filename = ""; bool bParsingComplete = false; CONTENTTYPES contentType = CONTENTTYPES.UNKNOWN; // Now Get the message while (currentLine < lines.Length) { switch (this._loc) { case PARSERLOCATION.PARSER_INBODY_MULTIPART_HEADER: if (contentType == CONTENTTYPES.UNKNOWN) { contentType = DetermineContentType(lines[currentLine]); } if (lines[currentLine].Contains("filename=") == true) { int iPos = lines[currentLine].IndexOf("filename=")+9; int iChars = lines[currentLine].Length - iPos; filename = lines[currentLine].Substring(iPos, iChars); filename = filename.Replace("\"", ""); filename = filename.Trim(); } else if (lines[currentLine] == "") { this._loc = PARSERLOCATION.PARSER_INBODY_MULTIPART_BODY; } break; case PARSERLOCATION.PARSER_INBODY_MULTIPART_BODY: // This looks for the boundary if (lines[currentLine].Length > 0) { if (_boundary.Length > 0) { // Look for the boundary if (lines[currentLine].Contains(_boundary)) { bParsingComplete = true; } } if (bParsingComplete == false) { part.Append(lines[currentLine]); part.Append("\r\n"); } } break; case PARSERLOCATION.PARSER_INBODY_SINGLEPART: if (lines[currentLine].Length > 0) { part.Append(lines[currentLine]); part.Append("\r\n"); } break; } if (bParsingComplete == true) { break; } currentLine++; } if (this._loc == PARSERLOCATION.PARSER_INBODY_SINGLEPART) { contentType = DetermineContentType(this._contentType); } AddPart(part.ToString(), contentType, filename, "base64"); }
private void ParseMultiPart(string[] lines, ref int currentLine) { StringBuilder part = new StringBuilder(); while (currentLine < lines.Length) { this._loc = PARSERLOCATION.PARSER_INBODY_MULTIPART_HEADER; ParsePart(lines, ref currentLine); currentLine++; } }