public async Task<List<Body>> GetBodies(IByteStreamReader streamReader, ContentTypeFieldValue contentType, string transportEncoding) { var reader = new BufferedByteReader(streamReader); switch (contentType.Type) { case "multipart": var bodyPartReader = new BodyReader(ContentType, Header.ContentTransferEncoding); var bodies = await bodyPartReader.ReadBody(reader); break; case "text": var textString = ""; using (var memstream = await streamReader.GetMemoryStreamAsync()) { using (var unEncoded = await MailMethods.UnEncode(memstream, Header.ContentTransferEncoding)) { using (var textReader = new StreamReader(unEncoded, Encoding.GetEncoding(Header.ContentType.Charset))) { textString = await textReader.ReadToEndAsync(); } } } var textBody = new Body(); textBody.Content = (new MemoryStream(Encoding.Unicode.GetBytes(textString))); break; case "image": case "audio": case "video": case "application": var binaryBody = new Body(); binaryBody.Content = await streamReader.GetMemoryStreamAsync()); break; } }
public static async Task <MemoryStream> UnEncode(MemoryStream encodedContent, string transferEncoding) { var unEncodedContent = new MemoryStream(); try { switch (transferEncoding.ToLower()) { case "7bit": case "8bit": case "binary": unEncodedContent = encodedContent; break; case "quoted-printable": unEncodedContent = MailMethods.FromQuotedPrintable(encodedContent); break; case "base64": var base64String = Encoding.ASCII.GetString(encodedContent.ToArray()); var bytes = Convert.FromBase64String(base64String); await unEncodedContent.WriteAsync(bytes, 0, bytes.Length); break; default: using (var writer = new StreamWriter(unEncodedContent, Encoding.ASCII)) { writer.WriteLine("Unknown 'Content-transfer-encoding' encounterd: {0}", transferEncoding); writer.WriteLine("------Encoded Content-------"); await writer.FlushAsync(); await encodedContent.CopyToAsync(unEncodedContent); } break; } return(unEncodedContent); } catch (Exception ex) { using (var writer = new StreamWriter(unEncodedContent, Encoding.ASCII)) { writer.WriteLine(ex.Message); writer.WriteLine("------Encoded Content-------"); await writer.FlushAsync(); await encodedContent.CopyToAsync(unEncodedContent); return(unEncodedContent); } } }