/// <summary> /// Interpreta el contenido de una sección /// </summary> private static string ParseContentSection(ParserLines objParser, string strBoundaryParent, string strBoundary, ref string strLastLine) { bool blnEnd = false; string strLine, strContent = ""; // Lee el contenido while (!blnEnd && !objParser.IsEof) { // Lee la línea strLine = objParser.ReadLine(); // Dependiendo de si estamos en una línea de boundary o en una línea de contenido if (IsStartBoundary(strLine, strBoundary) || IsStartBoundary(strLine, strBoundaryParent)) { // Guarda la última línea strLastLine = strLine; // Indica que ha terminado blnEnd = true; } else { // Añade el salto de línea si es necesario if (!string.IsNullOrEmpty(strContent)) strContent += "\r\n"; // Añade la línea al contenido strContent += strLine; } } // Devuelve el contenido de la sección return strContent; }
/// <summary> /// Lee las cabeceras del mensaje /// </summary> internal static void ReadHeaders(ParserLines objParser, MimeMessage objMessage) { // Lee las cabeceras objMessage.Headers.AddRange(ReadHeaders(objParser)); // Normaliza las cabeceras del mensaje NormalizeHeaders(objMessage); }
/// <summary> /// Lee las cabeceras de una sección /// </summary> internal static HeadersCollection ReadHeaders(ParserLines objParser, Section objSection) { HeadersCollection objColHeaders = ReadHeaders(objParser); // Normaliza las cabeceras NormalizeHeaders(objSection, objColHeaders); // Devuelve la colección de cabeceras return objColHeaders; }
/// <summary> /// Interpreta los datos de un mensaje /// </summary> public static MimeMessage Parse(string strMessage) { MimeMessage objMail = new MimeMessage(); ParserLines objParser = new ParserLines(strMessage); Section objSection; // Interpreta las líneas recibidas MimeHeadersParser.ReadHeaders(objParser, objMail); // Interpreta las secciones objSection = ParseSections(objMail.ContentType, objParser); // Normaliza el correo NormalizeMail(objMail, objSection); // Devuelve el mensaje recibido return objMail; }
/// <summary> /// Interpreta las secciones con tipo de contenido "multipart/report" /// </summary> private static SectionsCollection ParseSectionsMultipartReport(Section objSection, ParserLines objParser, ref string strLastLine) { return ParseSectionsChild(objSection.ContentType.Boundary, objParser, ref strLastLine); }
/// <summary> /// Interpreta las secciones hijo /// </summary> private static SectionsCollection ParseSectionsChild(string strBoundaryParent, ParserLines objParser, ref string strLastLine) { SectionsCollection objColSections = new SectionsCollection(); string strLine; // Lee la primera línea (o recupera la anterior if (!string.IsNullOrEmpty(strLastLine)) strLine = strLastLine; else strLine = objParser.ReadLine(); // Quita las líneas anteriores al boundary si es necesario if (!string.IsNullOrEmpty(strBoundaryParent) && !IsStartBoundary(strLine, strBoundaryParent)) { // Lee la siguiente línea while (!objParser.IsEof && !IsStartBoundary(strLine, strBoundaryParent)) strLine = objParser.ReadLine(); } // Si es la línea de boundary if (IsStartBoundary(strLine, strBoundaryParent)) { bool blnEnd = false; // Recorre las secciones hija while (!objParser.IsEof && !blnEnd) if (!string.IsNullOrEmpty(strLastLine) && IsEndBoundary(strLastLine, strBoundaryParent)) blnEnd = true; else { Section objSection = ParseSection(objParser, strBoundaryParent, ref strLastLine); // Añade la sección a la colección objColSections.Add(objSection); // Comprueba la última línea leída if (IsEndBoundary(strLastLine, strBoundaryParent)) { // Vacía la última línea al ser un final de sección para pasar a la siguiente lectura // sin una cadena de lectura atrasada (al fin y al cabo, la hemos tratado en el // IsEndBoundary if (!objParser.IsEof) { strLastLine = objParser.ReadLine(); if (strLastLine == "") strLastLine = objParser.ReadLine(); } else strLastLine = ""; // Indica que es el final de sección blnEnd = true; } else if (objSection.ContentType.IsMultipart) objSection.Sections.AddRange(ParseSectionsChild(objSection.ContentType.Boundary, objParser, ref strLastLine)); } } else throw new Exception("Boundary mal formado"); // Devuelve las secciones return objColSections; }
/// <summary> /// Interpreta las secciones /// </summary> private static Section ParseSections(ContentType objContentType, ParserLines objParser) { Section objSection = new Section(); // Asigna los parámetros a la sección objSection.ContentType = objContentType; // Interpreta la sección ParseSection(objSection, objParser); // Devuelve la sección interpretada return objSection; }
/// <summary> /// Interpreta la sección /// </summary> private static Section ParseSection(ParserLines objParser, string strBoundaryParent, ref string strLastLine) { Section objSection = new Section(); // Lee las cabeceras objSection.Headers = MimeHeadersParser.ReadHeaders(objParser, objSection); // Obtiene el contenido de la sección objSection.Content = ParseContentSection(objParser, strBoundaryParent, objSection.ContentType.Boundary, ref strLastLine); // Devuelve la sección return objSection; }
/// <summary> /// Interpreta los datos de una sección /// </summary> private static void ParseSection(Section objSection, ParserLines objParser) { string strLastLine = null; if (objSection.ContentType.IsMultipart) objSection.Sections.AddRange(ParseSectionsChild(objSection.ContentType.Boundary, objParser, ref strLastLine)); else if (objSection.ContentType.Type == ContentType.ContentTypeEnum.MultipartReport) objSection.Sections.AddRange(ParseSectionsMultipartReport(objSection, objParser, ref strLastLine)); else objSection.Content = ParseContentSection(objParser, null, null, ref strLastLine); }
/// <summary> /// Lee las cabeceras /// </summary> private static HeadersCollection ReadHeaders(ParserLines objParser) { HeadersCollection objColHeaders = new HeadersCollection(); string strLine = "----"; // Interpreta las líneas de cabecera while (!objParser.IsEof && !string.IsNullOrEmpty(strLine)) { // Obtiene la línea strLine = objParser.ReadLineContinuous(); // Interpreta la cabecera (si la hay) if (!string.IsNullOrEmpty(strLine)) { Header objHeader = ParseHeader(strLine); // Añade la cabecera objColHeaders.Add(objHeader); } } // Devuelve la colección de cabeceras return objColHeaders; }