// ----------------------- ToMimeString --------------------------- public string ToMimeString(string InCharSet) { string friendlyResults = null; string addressResults = null; string results = null; // message line encode the friendly name. if (Stringer.IsNotEmpty(FriendlyName)) { MimeHeaderLineBuilder friendly = new MimeHeaderLineBuilder( ); friendly.Traits .SetEncoderCharSet(InCharSet) .SetOtherEncodeChars("<>\"\'"); friendly.Append(FriendlyName); friendlyResults = friendly.ToEncodedString( ); } // message line encode the email address MimeHeaderLineBuilder lb = new MimeHeaderLineBuilder( ); lb.Traits .SetEncoderCharSet(InCharSet); lb.Append(" <" + Address + ">"); addressResults = lb.ToEncodedString( ); results = MimeCommon.ConcatMessageLine(lb.Traits, friendlyResults, addressResults); return(results); }
// ------------------------- ParseAddressString ------------------------ public static EmailAddress ParseAddressString(string InString) { TextTraits traits; traits = new TextTraits( ) .SetQuoteEncapsulation(QuoteEncapsulation.Escape); traits.DividerPatterns.AddDistinct( new string[] { " ", "\t" }, Text.Enums.DelimClassification.DividerSymbol); WordCursor bgnFriendly = null; WordCursor endFriendly = null; EmailAddress results = new EmailAddress( ); WordCursor csr = Scanner.PositionBeginWord(InString, traits); while (true) { // advance to the next word in the address string. csr = Scanner.ScanNextWord(InString, csr); if (csr.IsEndOfString) { break; } // the email address itself is <braced>. else if ((csr.Word.Class == WordClassification.ContentBraced) && (csr.Word.BraceChar == '<')) { results.Address = csr.Word.BracedText; } // comment in the email address string. else if ((csr.Word.Class == WordClassification.ContentBraced) && (csr.Word.BraceChar == '(')) { results.Comment = csr.Word.BracedText; results.Comment = MimeCommon.DecodeHeaderString_EncodedOnly(results.Comment); } // word part of the friendly name in the address. extend the word range of // the friendly string. else { if (bgnFriendly == null) { bgnFriendly = csr; } endFriendly = csr; } } // working from the word range, isolate the full friendly name string. string fullFriendly = null; if ((bgnFriendly != null) && (bgnFriendly == endFriendly)) { fullFriendly = bgnFriendly.Word.ToString( ); } else if (bgnFriendly != null) { int Bx = bgnFriendly.WordBx; int Ex = endFriendly.WordEx; fullFriendly = InString.Substring(Bx, Ex - Bx + 1); } // final decode of the friendly name. name could be quoted, could contain // encoded-words. if (fullFriendly != null) { fullFriendly = MimeCommon.DecodeHeaderString_QuotedEncodedEither(fullFriendly); } // the friendly name could actually be the email address. if (results.Address == null) { results.Address = fullFriendly; } else { results.FriendlyName = fullFriendly; } return(results); }