public static string Encode(string InValue, MimeHeaderLineTraits InHdrTraits) { QuotedPrintableTraits qpTraits = CalcQuotedPrintableTraits(InHdrTraits); string ec = QuotedPrintable.Encode(InValue, qpTraits); string results = "=?" + InHdrTraits.EncoderCharSet + "?Q?" + ec + "?="; return(results); }
// -------------------------- EncodeAsRequired ------------------------------ // encode the string if "RequiresEncoding". Otherwise, return the input string // as is. public static string EncodeAsRequired( string InValue, QuotedPrintableTraits InTraits) { if (QuotedPrintable.RequiresEncoding(InValue, InTraits) == true) { StringBuilder sb = new StringBuilder(InValue.Length * 2); sb.Append("=?" + InTraits.CharSet + "?Q?"); sb.Append(QuotedPrintable.Encode(InValue, InTraits)); sb.Append("?="); return(sb.ToString( )); } else { return(InValue); } }
/// <summary> /// Crack the component parts of the encoded-word string starting at InBx /// in the string. Return an object pair. pair.a is a bool set to false if the /// encoded-word string is not correctly formed. pair.b is a MimeEncodedWord object /// containing the component parts of the cracked word. /// </summary> /// <param name="InString"></param> /// <param name="InBx"></param> /// <returns></returns> public static BoolObjectPair CrackEncodedWord(string InString, int InBx) { int Fx, Ix, Lx, RemLx; string ws = null; MimeEncodedWord ew = new MimeEncodedWord( ); bool isEncodedWord = true; try { // isolate the next 80 chars from the string as a workspace ( encoded words are // limited to 75 chars or less ) ew.Bx = InBx; ws = AcCommon.PullString(InString, InBx, 80, null).ToLower( ); Ix = 0; // isolate the charset name Ix = 2; if (isEncodedWord == true) { RemLx = ws.Length - Ix; if (RemLx <= 3) { isEncodedWord = false; } else { Fx = ws.IndexOf("?q?", Ix); if (Fx == -1) { isEncodedWord = false; } else { Lx = Fx - Ix; ew.CharSet = InString.Substring(InBx + Ix, Lx); Ix = Fx + 3; } } } // quoted-printable encoded text runs until "?=" if (isEncodedWord == true) { RemLx = ws.Length - Ix; if (RemLx <= 2) { isEncodedWord = false; } else { Fx = ws.IndexOf("?=", Ix); if (Fx == -1) { isEncodedWord = false; } else { Lx = Fx - Ix; string qpEncoded = InString.Substring(InBx + Ix, Lx); ew.DecodedValue = QuotedPrintable.DecodeString(qpEncoded); ew.Lx = Fx + 2; } } } } catch (Exception) { isEncodedWord = false; } return(new BoolObjectPair(isEncodedWord, ew)); }