public static IEnumerable <MultipartPart> GetParts(Mime.UnbufferedReader streamReader, Encoding customEncoding = null) { long startPosition = streamReader.BaseStream.Position; //find out the boundary and call the GetParts with boundary System.Collections.Specialized.NameValueCollection attributes; MultipartPart.ReadHeaderAttributes(streamReader.BaseStream, streamReader.BaseStream.Position, out attributes); if (attributes["charset"] != null) { try { customEncoding = Encoding.GetEncoding(attributes["charset"]); } catch { } } string boundary = attributes["boundary"]; if (boundary != null) { streamReader.BaseStream.Position = startPosition; //int partsReturned = 0; foreach (MultipartPart part in GetParts(streamReader, boundary, customEncoding)) { yield return(part); //partsReturned++; } //if(partsReturned == 0)//return a single part // yield return new MultipartPart(streamReader.BaseStream, streamReader.BaseStream.Position, (int)(streamReader.BaseStream.Length - streamReader.BaseStream.Position), customEncoding); } else { //return a single part yield return(new MultipartPart(streamReader.BaseStream, streamReader.BaseStream.Position, (int)(streamReader.BaseStream.Length - streamReader.BaseStream.Position), customEncoding)); //yield break; } }
public static IEnumerable <MultipartPart> GetParts(Mime.UnbufferedReader streamReader, bool useOnlyASCII, Encoding customEncoding = null) { long startPosition = streamReader.BaseStream.Position; //find out the boundary and call the GetParts with boundary System.Collections.Specialized.NameValueCollection attributes; MultipartPart.ReadHeaderAttributes(streamReader.BaseStream, streamReader.BaseStream.Position, out attributes, useOnlyASCII, null); SharedUtils.Logger.Log("Extracted attributes at offset " + startPosition + " : " + String.Join(",", attributes.AllKeys), SharedUtils.Logger.EventLogEntryType.Information); if (attributes["charset"] != null) { try { customEncoding = Encoding.GetEncoding(attributes["charset"]); } catch (Exception e) { SharedUtils.Logger.Log("Error getting encoding for charset \"" + attributes["charset"] + "\" in MIME part. " + e.Message, SharedUtils.Logger.EventLogEntryType.Warning); } } string boundary = attributes["boundary"]; if (boundary != null) { streamReader.BaseStream.Position = startPosition; //int partsReturned = 0; foreach (MultipartPart part in GetParts(streamReader, boundary, useOnlyASCII, customEncoding)) { SharedUtils.Logger.Log("MIME part extracted at offset " + startPosition + " with boundary \"" + boundary + "\"", SharedUtils.Logger.EventLogEntryType.Information); yield return(part); //partsReturned++; } //if(partsReturned == 0)//return a single part // yield return new MultipartPart(streamReader.BaseStream, streamReader.BaseStream.Position, (int)(streamReader.BaseStream.Length - streamReader.BaseStream.Position), customEncoding); } else { SharedUtils.Logger.Log("Extracting MIME part at offset " + streamReader.BaseStream.Position + " with no boundary", SharedUtils.Logger.EventLogEntryType.Information); //return a single part yield return(new MultipartPart(streamReader.BaseStream, streamReader.BaseStream.Position, (int)(streamReader.BaseStream.Length - streamReader.BaseStream.Position), customEncoding, useOnlyASCII)); //yield break; } }
public static IEnumerable <MultipartPart> GetParts(Mime.UnbufferedReader streamReader) { long startPosition = streamReader.BaseStream.Position; //find out the boundary and call the GetParts with boundary System.Collections.Specialized.NameValueCollection attributes; MultipartPart.ReadHeaderAttributes(streamReader.BaseStream, streamReader.BaseStream.Position, out attributes); string boundary = attributes["boundary"]; if (boundary != null) { streamReader.BaseStream.Position = startPosition; foreach (MultipartPart part in GetParts(streamReader, boundary)) { yield return(part); } } else { //return a single part yield return(new MultipartPart(streamReader.BaseStream, streamReader.BaseStream.Position, (int)(streamReader.BaseStream.Length - streamReader.BaseStream.Position))); //yield break; } }