/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset, bool reEncode) { if (!reEncode && m_ParseValue != null) { return(m_ParseValue); } else { if (wordEncoder != null) { return(m_Name + ": " + wordEncoder.Encode(m_Value) + "\r\n"); } else { return(m_Name + ": " + m_Value + "\r\n"); } } }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal protected override void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset, bool headerReencode) { if (stream == null) { throw new ArgumentNullException("stream"); } /* RFC 2046 5.1.1. * NOTE: The CRLF preceding the boundary delimiter line is conceptually * attached to the boundary so that it is possible to have a part that * does not end with a CRLF (line break). */ // Set "preamble" text if any. if (!string.IsNullOrEmpty(m_TextPreamble)) { byte[] preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble); stream.Write(preableBytes, 0, preableBytes.Length); } for (int i = 0; i < m_pBodyParts.Count; i++) { MIME_Entity bodyPart = m_pBodyParts[i]; // Start new body part. byte[] bStart = Encoding.UTF8.GetBytes("\r\n--" + this.Entity.ContentType.Param_Boundary + "\r\n"); stream.Write(bStart, 0, bStart.Length); bodyPart.ToStream(stream, headerWordEncoder, headerParmetersCharset, headerReencode); // Last body part, close boundary. if (i == (m_pBodyParts.Count - 1)) { byte[] bEnd = Encoding.UTF8.GetBytes("\r\n--" + this.Entity.ContentType.Param_Boundary + "--\r\n"); stream.Write(bEnd, 0, bEnd.Length); } } // Set "epilogoue" text if any. if (!string.IsNullOrEmpty(m_TextEpilogue)) { byte[] epilogoueBytes = Encoding.UTF8.GetBytes(m_TextEpilogue); stream.Write(epilogoueBytes, 0, epilogoueBytes.Length); } }
/// <summary> /// Parses header field from the specified value. /// </summary> /// <param name="value">Header field value. Header field name must be included. For example: 'Content-Type: text/plain'.</param> /// <returns>Returns parsed header field.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>value</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when header field parsing errors.</exception> public static MIME_h_Unstructured Parse(string value) { if (value == null) { throw new ArgumentNullException("value"); } MIME_h_Unstructured retVal = new MIME_h_Unstructured(); string[] name_value = value.Split(new char[] { ':' }, 2); if (name_value[0].Trim() == string.Empty) { throw new ParseException("Invalid header field '" + value + "' syntax."); } retVal.m_Name = name_value[0]; retVal.m_Value = MIME_Encoding_EncodedWord.DecodeTextS(MIME_Utils.UnfoldHeader(name_value.Length == 2 ? name_value[1].TrimStart() : "")); retVal.m_ParseValue = value; return(retVal); }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal protected abstract void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset, bool headerReencode);
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset, bool reEncode) { return(m_ParseValue); }
/// <summary> /// Returns MIME header as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns MIME header as string.</returns> public string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { return(ToString(wordEncoder, parmetersCharset, false)); }
/// <summary> /// Returns header field value as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded. /// If encoding needed, UTF-8 is strongly reccomended if not sure.</param> /// <returns>Returns header field value as string.</returns> public string ValueToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { return(ToString(wordEncoder, parmetersCharset).Split(new char[] { ':' }, 2)[1].TrimStart()); }
/// <summary> /// Stores header to the specified stream. /// </summary> /// <param name="stream">Stream where to store header.</param> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> public void ToStream(Stream stream, MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { ToStream(stream, wordEncoder, parmetersCharset, false); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded. /// If encoding needed, UTF-8 is strongly reccomended if not sure.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public abstract string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset, bool reEncode);
/// <summary> /// Stores MIME entity to the specified file. /// </summary> /// <param name="file">File name with path where to store MIME entity.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>file</b> is null.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> public void ToFile(string file, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { ToFile(file, headerWordEncoder, headerParmetersCharset, false); }
/// <summary> /// Returns MIME entity as byte[]. /// </summary> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns MIME entity as byte[].</returns> public byte[] ToByte(MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { return(ToByte(headerWordEncoder, headerParmetersCharset, false)); }
/// <summary> /// Returns MIME entity as string. /// </summary> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns MIME entity as string.</returns> public string ToString(MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { return(ToString(headerWordEncoder, headerParmetersCharset, false)); }
/// <summary> /// Store MIME enity to the specified stream. /// </summary> /// <param name="stream">Stream where to store MIME entity. Storing starts form stream current position.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null.</exception> public void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { ToStream(stream, headerWordEncoder, headerParmetersCharset, false); }