예제 #1
0
        /// <summary>
        /// Get the decoded text and the encoding used to convert it into unicode.
        /// </summary>
        /// <remarks>
        /// <para>If the charset parameter on the <see cref="MimeEntity.ContentType"/>
        /// is set, it will be used in order to convert the raw content into unicode.
        /// If that fails or if the charset parameter is not set, the first 2 bytes of
        /// the content will be checked for a unicode BOM. If a BOM exists, then that
        /// will be used for conversion. If no BOM is found, then UTF-8 is attempted.
        /// If conversion fails, then iso-8859-1 will be used as the final fallback.</para>
        /// <para>For more control, use <see cref="GetText(Encoding)"/>
        /// or <see cref="GetText(String)"/>.</para>
        /// </remarks>
        /// <param name="encoding">The encoding used to convert the text into unicode.</param>
        /// <returns>The decoded text.</returns>
        public string GetText(out Encoding encoding)
        {
            if (Content == null)
            {
                encoding = Encoding.ASCII;
                return(string.Empty);
            }

            encoding = ContentType.CharsetEncoding;

            if (encoding == null)
            {
                try {
                    using (var content = Content.Open()) {
                        if (!CharsetUtils.TryGetBomEncoding(content, out encoding))
                        {
                            encoding = CharsetUtils.UTF8;
                        }
                    }

                    return(GetText(encoding));
                } catch (DecoderFallbackException) {
                    // fall back to iso-8859-1
                    encoding = CharsetUtils.Latin1;
                }
            }

            return(GetText(encoding));
        }
예제 #2
0
        /// <summary>
        /// Get a text preview of the text part.
        /// </summary>
        /// <remarks>
        /// Gets a text preview of the text part.
        /// </remarks>
        /// <param name="body">The text part.</param>
        /// <returns>A string representing a shortened preview of the original text.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="body"/> is <c>null</c>.
        /// </exception>
        public static string GetPreviewText(TextPart body)
        {
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }

            if (body.Content == null)
            {
                return(string.Empty);
            }

            var encoding = body.ContentType.CharsetEncoding;
            var buffer   = new byte[16 * 1024];
            int nread    = 0;

            using (var content = body.Content.Open()) {
                int n;

                do
                {
                    if ((n = content.Read(buffer, nread, buffer.Length - nread)) <= 0)
                    {
                        break;
                    }

                    nread += n;
                } while (nread < buffer.Length);
            }

            if (encoding == null)
            {
                if (!CharsetUtils.TryGetBomEncoding(buffer, nread, out encoding))
                {
                    encoding = CharsetUtils.UTF8;
                }
            }

            using (var stream = new MemoryStream(buffer, 0, nread, false)) {
                var previewer = Create(body.Format);

                try {
                    return(previewer.GetPreviewText(stream, encoding));
                } catch (DecoderFallbackException) {
                    stream.Position = 0;

                    return(previewer.GetPreviewText(stream, CharsetUtils.Latin1));
                }
            }
        }