/// <summary> /// Glue method to create a bodypart from a MIMEPart instance. /// </summary> /// <param name="mimePart">The MIMEPart instance to create the bodypart instance from.</param> /// <returns>An initialized instance of the Bodypart class.</returns> Bodypart BodypartFromMIME(MIMEPart mimePart) { NameValueCollection contentType = ParseMIMEField( mimePart.Headers["Content-Type"]); Bodypart p = new Bodypart(null); Match m = Regex.Match(contentType["value"], "(.+)/(.+)"); if (m.Success) { p.Type = ContentTypeMap.fromString(m.Groups[1].Value); p.Subtype = m.Groups[2].Value; } p.Encoding = ContentTransferEncodingMap.fromString( mimePart.Headers["Content-Transfer-Encoding"]); p.Id = mimePart.Headers["Content-Id"]; foreach (string k in contentType.AllKeys) { p.Parameters.Add(k, contentType[k]); } p.Size = mimePart.MessageText.Length; if (mimePart.Headers["Content-Disposition"] != null) { NameValueCollection disposition = ParseMIMEField( mimePart.Headers["Content-Disposition"]); p.Disposition.Type = ContentDispositionTypeMap.fromString( disposition["value"]); p.Disposition.Filename = disposition["Filename"]; foreach (string k in disposition.AllKeys) { p.Disposition.Attributes.Add(k, disposition[k]); } } return(p); }
MIMEPart[] ParseMIMEParts(StringReader reader, string boundary) { List <MIMEPart> list = new List <MIMEPart>(); string start = "--" + boundary, end = "--" + boundary + "--", line; // Skip everything up to the first boundary. while ((line = reader.ReadLine()) != null) { if (line.StartsWith(start)) { break; } } // Read the MIME parts which are delimited by boundary strings. while (line != null && line.StartsWith(start)) { MIMEPart p = new MIMEPart(); // Read the part header. StringBuilder header = new StringBuilder(); while (!String.IsNullOrEmpty(line = reader.ReadLine())) { header.AppendLine(line); } p.Headers = ParseMailHeader(header.ToString()); // Account for nested multipart content. NameValueCollection contentType = ParseMIMEField(p.Headers["Content-Type"]); if (contentType["Boundary"] != null) { list.AddRange(ParseMIMEParts(reader, contentType["boundary"])); } // Read the part body. StringBuilder body = new StringBuilder(); while ((line = reader.ReadLine()) != null) { if (line.StartsWith(start)) { break; } body.AppendLine(line); } p.MessageText = body.ToString(); // Add the MIME part to the list unless body is null or empty which means the body // contained nested multipart content. if (p.MessageText != null && p.MessageText.Trim() != String.Empty) { list.Add(p); } // If this boundary is the end boundary, we're done. if (line == null || line.StartsWith(end)) { break; } } return(list.ToArray()); }
/// <summary> /// Glue method to create a bodypart from a MIMEPart instance. /// </summary> /// <param name="mimePart">The MIMEPart instance to create the bodypart instance from.</param> /// <returns>An initialized instance of the Bodypart class.</returns> Bodypart BodypartFromMIME(MIMEPart mimePart) { NameValueCollection contentType = ParseMIMEField( mimePart.Headers["Content-Type"]); Bodypart p = new Bodypart(null); Match m = Regex.Match(contentType["value"], "(.+)/(.+)"); if (m.Success) { p.Type = ContentTypeMap.fromString(m.Groups[1].Value); p.Subtype = m.Groups[2].Value; } p.Encoding = ContentTransferEncodingMap.fromString( mimePart.Headers["Content-Transfer-Encoding"]); p.Id = mimePart.Headers["Content-Id"]; foreach (string k in contentType.AllKeys) p.Parameters.Add(k, contentType[k]); p.Size = mimePart.MessageText.Length; if (mimePart.Headers["Content-Disposition"] != null) { NameValueCollection disposition = ParseMIMEField( mimePart.Headers["Content-Disposition"]); p.Disposition.Type = ContentDispositionTypeMap.fromString( disposition["value"]); p.Disposition.Filename = disposition["Filename"]; foreach (string k in disposition.AllKeys) p.Disposition.Attributes.Add(k, disposition[k]); } return p; }
MIMEPart[] ParseMIMEParts(StringReader reader, string boundary) { List<MIMEPart> list = new List<MIMEPart>(); string start = "--" + boundary, end = "--" + boundary + "--", line; // Skip everything up to the first boundary. while ((line = reader.ReadLine()) != null) { if (line.StartsWith(start)) break; } // Read the MIME parts which are delimited by boundary strings. while (line != null && line.StartsWith(start)) { MIMEPart p = new MIMEPart(); // Read the part header. StringBuilder header = new StringBuilder(); while (!String.IsNullOrEmpty(line = reader.ReadLine())) header.AppendLine(line); p.Headers = ParseMailHeader(header.ToString()); // Account for nested multipart content. NameValueCollection contentType = ParseMIMEField(p.Headers["Content-Type"]); if (contentType["Boundary"] != null) list.AddRange(ParseMIMEParts(reader, contentType["boundary"])); // Read the part body. StringBuilder body = new StringBuilder(); while ((line = reader.ReadLine()) != null) { if (line.StartsWith(start)) break; body.AppendLine(line); } p.MessageText = body.ToString(); // Add the MIME part to the list unless body is null or empty which means the body // contained nested multipart content. if (p.MessageText != null && p.MessageText.Trim() != String.Empty) list.Add(p); // If this boundary is the end boundary, we're done. if (line == null || line.StartsWith(end)) break; } return list.ToArray(); }
private static string DecodeMessage(MIMEPart filterMessage) { //Automation.Common.Log.Write(Automation.Common.Log.Modules.Player, Automation.Common.Log.LogTypes.INFORMATION, "EmailParser: DecodeMessage", "Entry"); string content = string.Empty; if (filterMessage != null) { string contentType = filterMessage.Headers["Content-Type"]; string charset = "us-ascii"; // default charset string contentTransferEncoding = filterMessage.Headers["Content-Transfer-Encoding"]; Match m = CharsetRegex.Match(contentType); if (m.Success) charset = m.Groups["charset"].Value; content = (contentTransferEncoding != null) ? DecodeContent(contentTransferEncoding, filterMessage.MessageText, charset) : filterMessage.MessageText; } //Automation.Common.Log.Write(Automation.Common.Log.Modules.Player, Automation.Common.Log.LogTypes.INFORMATION, "EmailParser: DecodeMessage", "Exit"); return content; }