public void SimpleTest() { //String setofchars=Get256Chars(); String setofchars = "" + (char)0x01 + (char)0x02; QPEncoder qpencoder = QPEncoder.GetInstance(); StringReader sr = new StringReader(setofchars); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); qpencoder.Encode(sr, sw, System.Text.Encoding.GetEncoding("iso-8859-1")); Assert.AreEqual("=01=02", sb.ToString()); }
public void LineLengthTest() { String line = "12345678901234567890123456789012345678901234567890123456789012345678901234567890"; String expectedresult = "1234567890123456789012345678901234567890123456789012345678901234567890123456=\r\n7890"; QPEncoder qpencoder = QPEncoder.GetInstance(); StringReader sr = new StringReader(line); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); qpencoder.Encode(sr, sw, System.Text.Encoding.GetEncoding("iso-8859-1")); Assert.AreEqual(expectedresult, sb.ToString()); Assert.AreEqual(expectedresult, qpencoder.EncodeString(line, System.Text.Encoding.GetEncoding("iso-8859-1"))); }
/// <summary> /// Encodes a string in "Mime Word" /// </summary> /// <param name="Src">The string to encode</param> /// <param name="Encoding">The string charset Encoding</param> /// <param name="TransfertEncoding">The transfert encoding</param> /// <param name="Maxlength">Maximum number of char beetween 2 CRLF</param> /// /// <returns>A MimeWord encoded string</returns> public static string Encode(string Src, System.Text.Encoding Encoding, EncodingIdentifier TransfertEncoding, int Maxlength) { StringBuilder st = new StringBuilder(Src.Length); switch (TransfertEncoding) { case EncodingIdentifier.QuotedPrintable: { string suffix = "?="; string prefix = "=?" + Encoding.BodyName + "?q?"; int len = prefix.Length + suffix.Length; st.Append(prefix); for (int i = 0; i < Src.Length; i++) { string tmp = QPEncoder.Encode(Src[i], Encoding); if (len + tmp.Length >= Maxlength) { st.AppendFormat("{0}\r\n", suffix); len = prefix.Length + suffix.Length; st.Append(prefix); } st.Append(tmp); len += tmp.Length; } st.Append("?="); } break; case EncodingIdentifier.Base64: st.AppendFormat("=?{0}?b?{1}?=", Encoding.BodyName, MimeEncoder.StringBase64(Src, Encoding, 78)); break; default: throw (new ArgumentOutOfRangeException("TransfertEncoding", TransfertEncoding, "Invalid transfert identifier")); } return(st.ToString()); }