Exemple #1
0
        /// <summary>
        /// Converts the TNEF content into a <see cref="MimeMessage"/>.
        /// </summary>
        /// <remarks>
        /// TNEF data often contains properties that map to <see cref="MimeMessage"/>
        /// headers. TNEF data also often contains file attachments which will be
        /// mapped to MIME parts.
        /// </remarks>
        /// <returns>A message representing the TNEF data in MIME format.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// The <see cref="MimePart.Content"/> property is <c>null</c>.
        /// </exception>
        public MimeMessage ConvertToMessage()
        {
            if (Content == null)
                throw new InvalidOperationException ("Cannot parse null TNEF data.");

            int codepage = 0;

            if (!string.IsNullOrEmpty (ContentType.Charset)) {
                if ((codepage = CharsetUtils.GetCodePage (ContentType.Charset)) == -1)
                    codepage = 0;
            }

            using (var reader = new TnefReader (Content.Open (), codepage, TnefComplianceMode.Loose)) {
                return ExtractTnefMessage (reader);
            }
        }
        public void TestArgumentExceptions()
        {
            var buffer = new byte[10];

            Assert.Throws <ArgumentNullException> (() => CharsetUtils.ConvertToUnicode((ParserOptions)null, buffer, 0, buffer.Length));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.ConvertToUnicode(ParserOptions.Default, null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => CharsetUtils.ConvertToUnicode(ParserOptions.Default, buffer, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => CharsetUtils.ConvertToUnicode(ParserOptions.Default, buffer, 0, -1));

            Assert.Throws <ArgumentNullException> (() => CharsetUtils.ConvertToUnicode((Encoding)null, buffer, 0, buffer.Length));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.ConvertToUnicode(Encoding.UTF8, null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => CharsetUtils.ConvertToUnicode(Encoding.UTF8, buffer, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => CharsetUtils.ConvertToUnicode(Encoding.UTF8, buffer, 0, -1));

            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetCodePage(null));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetEncoding(null));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetEncoding(null, "fallback"));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetEncoding("charset", null));
            Assert.Throws <ArgumentOutOfRangeException> (() => CharsetUtils.GetEncoding(-1, "fallback"));
            Assert.Throws <ArgumentNullException> (() => CharsetUtils.GetEncoding(28591, null));
        }
Exemple #3
0
        public void TestCharsetUtilsGetCodePage()
        {
            for (int i = 1; i < 16; i++)
            {
                int    expected, codepage;
                string name;

                name     = string.Format("iso-8859-{0}", i);
                codepage = CharsetUtils.GetCodePage(name);

                switch (i)
                {
                case 11:
                    expected = 874;
                    break;

                case 10:
                case 12:
                case 14:
                    expected = -1;
                    break;

                case 13:
                    if (Path.DirectorySeparatorChar == '\\')
                    {
                        goto default;
                    }
                    expected = -1;
                    break;

                default:
                    expected = 28590 + i;
                    break;
                }

                Assert.AreEqual(expected, codepage, "Invalid codepage for: {0}", name);
            }

            for (int i = 0; i < 10; i++)
            {
                int    expected, codepage;
                string name;

                if (i < 9)
                {
                    expected = 1250 + i;
                }
                else
                {
                    expected = -1;
                }

                name     = string.Format("windows-125{0}", i);
                codepage = CharsetUtils.GetCodePage(name);

                Assert.AreEqual(expected, codepage, "Invalid codepage for: {0}", name);

                name     = string.Format("windows-cp125{0}", i);
                codepage = CharsetUtils.GetCodePage(name);

                Assert.AreEqual(expected, codepage, "Invalid codepage for: {0}", name);

                name     = string.Format("cp125{0}", i);
                codepage = CharsetUtils.GetCodePage(name);

                Assert.AreEqual(expected, codepage, "Invalid codepage for: {0}", name);
            }

            foreach (var ibm in new int[] { 850, 852, 855, 857, 860, 861, 862, 863 })
            {
                int    codepage;
                string name;

                name     = string.Format("ibm-{0}", ibm);
                codepage = CharsetUtils.GetCodePage(name);

                Assert.AreEqual(ibm, codepage, "Invalid codepage for: {0}", name);
            }
        }