/// <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>
        /// <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)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // Set "preamble" text if any.
            if (!string.IsNullOrEmpty(m_TextPreamble))
            {
                byte[] preableBytes = null;
                if (m_TextPreamble.EndsWith("\r\n"))
                {
                    preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble);
                }
                else
                {
                    preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble + "\r\n");
                }
                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("--" + this.ContentType.Param_Boundary + "\r\n");
                stream.Write(bStart, 0, bStart.Length);

                bodyPart.ToStream(stream, headerWordEncoder, headerParmetersCharset);

                // Last body part, close boundary.
                if (i == (m_pBodyParts.Count - 1))
                {
                    byte[] bEnd = Encoding.UTF8.GetBytes("--" + this.ContentType.Param_Boundary + "--\r\n");
                    stream.Write(bEnd, 0, bEnd.Length);
                }
            }

            // Set "epilogoue" text if any.
            if (!string.IsNullOrEmpty(m_TextEpilogue))
            {
                byte[] epilogoueBytes = null;
                if (m_TextEpilogue.EndsWith("\r\n"))
                {
                    epilogoueBytes = Encoding.UTF8.GetBytes(m_TextEpilogue);
                }
                else
                {
                    epilogoueBytes = Encoding.UTF8.GetBytes(m_TextEpilogue + "\r\n");
                }
                stream.Write(epilogoueBytes, 0, epilogoueBytes.Length);
            }
        }
Exemple #2
0
        /// <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>
        /// 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).
             *
             *  dash-boundary := "--" boundary
             *
             *  multipart-body := [preamble CRLF]
             *                    dash-boundary transport-padding CRLF
             *                    body-part *encapsulation
             *                    close-delimiter transport-padding
             *                    [CRLF epilogue]
             *
             *  encapsulation := delimiter transport-padding
             *                   CRLF body-part
             *
             *  delimiter := CRLF dash-boundary
             *
             *  close-delimiter := delimiter "--"
             */

            // Set "preamble" text if any.
            if (!string.IsNullOrEmpty(m_TextPreamble))
            {
                if (m_TextPreamble.EndsWith("\r\n"))
                {
                    byte[] preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble);
                    stream.Write(preableBytes, 0, preableBytes.Length);
                }
                else
                {
                    byte[] preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble + "\r\n");
                    stream.Write(preableBytes, 0, preableBytes.Length);
                }
            }

            for (int i = 0; i < m_pBodyParts.Count; i++)
            {
                MIME_Entity bodyPart = m_pBodyParts[i];

                // First boundary has no preceeding CRLF.
                if (i == 0)
                {
                    byte[] bStart = Encoding.UTF8.GetBytes("--" + this.Entity.ContentType.Param_Boundary + "\r\n");
                    stream.Write(bStart, 0, bStart.Length);
                }
                // Boundaries > 1.
                else
                {
                    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 + "--");
                    stream.Write(bEnd, 0, bEnd.Length);
                }
            }

            // Set "epilogoue" text if any.
            if (!string.IsNullOrEmpty(m_TextEpilogue))
            {
                if (m_TextEpilogue.StartsWith("\r\n"))
                {
                    byte[] epilogoueBytes = Encoding.UTF8.GetBytes(m_TextEpilogue);
                    stream.Write(epilogoueBytes, 0, epilogoueBytes.Length);
                }
                else
                {
                    byte[] epilogoueBytes = Encoding.UTF8.GetBytes("\r\n" + m_TextEpilogue);
                    stream.Write(epilogoueBytes, 0, epilogoueBytes.Length);
                }
            }
        }