/// <summary> /// Decodes a byte array into another byte array based upon the Content Transfer encoding /// </summary> /// <param name="messageBody">The byte array to decode into another byte array</param> /// <param name="contentTransferEncoding">The <see cref="ContentTransferEncoding"/> of the byte array</param> /// <returns>A byte array which comes from the <paramref name="contentTransferEncoding"/> being used on the <paramref name="messageBody"/></returns> /// <exception cref="ArgumentNullException">If <paramref name="messageBody"/> is <see langword="null"/></exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if the <paramref name="contentTransferEncoding"/> is unsupported</exception> private static byte[] DecodeBody(byte[] messageBody, ContentTransferEncoding contentTransferEncoding) { if (messageBody == null) { throw new ArgumentNullException("messageBody"); } switch (contentTransferEncoding) { case ContentTransferEncoding.QuotedPrintable: // If encoded in QuotedPrintable, everything in the body is in US-ASCII return(QuotedPrintable.DecodeContentTransferEncoding(Encoding.ASCII.GetString(messageBody))); case ContentTransferEncoding.Base64: // If encoded in Base64, everything in the body is in US-ASCII return(Base64.Decode(Encoding.ASCII.GetString(messageBody))); case ContentTransferEncoding.SevenBit: case ContentTransferEncoding.Binary: case ContentTransferEncoding.EightBit: // We do not have to do anything return(messageBody); default: throw new ArgumentOutOfRangeException("contentTransferEncoding"); } }
public void TestUnderscoresNotConvertedToSpacesInContentTransferEncoding() { const string input = "a_b_c_d_e_f"; const string expectedOutput = "a_b_c_d_e_f"; string output = Encoding.ASCII.GetString(QuotedPrintable.DecodeContentTransferEncoding(input)); Assert.AreEqual(expectedOutput, output); }