public InternetMailMessage(HeaderList headers) : this()
        {
            using (var sw = new MemoryStream())
            {
                headers.WriteTo(sw);
                sw.Position = 0;

                using (var tr = new StreamReader(sw))
                {
                    Header = tr.ReadToEnd();
                }
            }

            foreach (var header in headers)
            {
                SetField(header.Id.ToHeaderName(), ValueFactory.Create(header.Value));
            }

            DateReceived = DateTime.Now;
        }
Exemple #2
0
        public void TestArgumentExceptions()
        {
            var    list = new HeaderList();
            Header header;
            string value;

            using (var stream = new MemoryStream()) {
                Assert.Throws <ArgumentNullException> (() => HeaderList.Load(null, "filename.txt"));
                Assert.Throws <ArgumentNullException> (() => HeaderList.Load(ParserOptions.Default, (string)null));

                Assert.Throws <ArgumentNullException> (() => HeaderList.Load(null, stream));
                Assert.Throws <ArgumentNullException> (() => HeaderList.Load(ParserOptions.Default, (Stream)null));

                Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(null, "filename.txt"));
                Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(ParserOptions.Default, (string)null));

                Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(null, stream));
                Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(ParserOptions.Default, (Stream)null));
            }

            // Add
            Assert.Throws <ArgumentNullException> (() => list.Add(null));
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Add(HeaderId.Unknown, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Add(HeaderId.AdHoc, null));
            Assert.Throws <ArgumentNullException> (() => list.Add(null, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Add("field", null));
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Add(HeaderId.Unknown, Encoding.UTF8, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Add(HeaderId.AdHoc, null, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Add(HeaderId.AdHoc, Encoding.UTF8, null));
            Assert.Throws <ArgumentNullException> (() => list.Add(null, Encoding.UTF8, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Add("field", null, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Add("field", Encoding.UTF8, null));

            // Contains
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Contains(HeaderId.Unknown));
            Assert.Throws <ArgumentNullException> (() => list.Contains((Header)null));
            Assert.Throws <ArgumentNullException> (() => list.Contains((string)null));

            // CopyTo
            Assert.Throws <ArgumentOutOfRangeException> (() => list.CopyTo(new Header[0], -1));
            Assert.Throws <ArgumentNullException> (() => list.CopyTo(null, 0));

            // IndexOf
            Assert.Throws <ArgumentOutOfRangeException> (() => list.IndexOf(HeaderId.Unknown));
            Assert.Throws <ArgumentNullException> (() => list.IndexOf((Header)null));
            Assert.Throws <ArgumentNullException> (() => list.IndexOf((string)null));

            // Insert
            list.Add("field", "value");
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(-1, new Header(HeaderId.AdHoc, "value")));
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(-1, HeaderId.AdHoc, Encoding.UTF8, "value"));
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(-1, "field", Encoding.UTF8, "value"));
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(-1, HeaderId.AdHoc, "value"));
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(-1, "field", "value"));
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(0, HeaderId.Unknown, Encoding.UTF8, "value"));
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Insert(0, HeaderId.Unknown, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Insert(0, HeaderId.AdHoc, Encoding.UTF8, null));
            Assert.Throws <ArgumentNullException> (() => list.Insert(0, HeaderId.AdHoc, null, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Insert(0, HeaderId.AdHoc, null));
            Assert.Throws <ArgumentNullException> (() => list.Insert(0, null, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Insert(0, "field", null));
            Assert.Throws <ArgumentNullException> (() => list.Insert(0, null));

            // LastIndexOf
            Assert.Throws <ArgumentOutOfRangeException> (() => list.LastIndexOf(HeaderId.Unknown));
            Assert.Throws <ArgumentNullException> (() => list.LastIndexOf((string)null));

            // Remove
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Remove(HeaderId.Unknown));
            Assert.Throws <ArgumentNullException> (() => list.Remove((Header)null));
            Assert.Throws <ArgumentNullException> (() => list.Remove((string)null));

            // RemoveAll
            Assert.Throws <ArgumentOutOfRangeException> (() => list.RemoveAll(HeaderId.Unknown));
            Assert.Throws <ArgumentNullException> (() => list.RemoveAll((string)null));

            // RemoveAt
            Assert.Throws <ArgumentOutOfRangeException> (() => list.RemoveAt(-1));

            // Replace
            Assert.Throws <ArgumentNullException> (() => list.Replace(null));
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Replace(HeaderId.Unknown, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Replace(HeaderId.AdHoc, null));
            Assert.Throws <ArgumentNullException> (() => list.Replace(null, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Replace("field", null));
            Assert.Throws <ArgumentOutOfRangeException> (() => list.Replace(HeaderId.Unknown, Encoding.UTF8, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Replace(HeaderId.AdHoc, null, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Replace(HeaderId.AdHoc, Encoding.UTF8, null));
            Assert.Throws <ArgumentNullException> (() => list.Replace(null, Encoding.UTF8, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Replace("field", null, "value"));
            Assert.Throws <ArgumentNullException> (() => list.Replace("field", Encoding.UTF8, null));

            using (var stream = new MemoryStream()) {
                // Load
                Assert.Throws <ArgumentNullException> (() => HeaderList.Load(ParserOptions.Default, (Stream)null));
                Assert.Throws <ArgumentNullException> (() => HeaderList.Load(ParserOptions.Default, (string)null));
                Assert.Throws <ArgumentNullException> (() => HeaderList.Load(null, stream));
                Assert.Throws <ArgumentNullException> (() => HeaderList.Load((Stream)null));
                Assert.Throws <ArgumentNullException> (() => HeaderList.Load((string)null));

                // LoadAsync
                Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(ParserOptions.Default, (Stream)null));
                Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(ParserOptions.Default, (string)null));
                Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync(null, stream));
                Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync((Stream)null));
                Assert.ThrowsAsync <ArgumentNullException> (async() => await HeaderList.LoadAsync((string)null));

                // WriteTo
                Assert.Throws <ArgumentNullException> (() => list.WriteTo(FormatOptions.Default, null));
                Assert.Throws <ArgumentNullException> (() => list.WriteTo(null, stream));
                Assert.Throws <ArgumentNullException> (() => list.WriteTo(null));

                // WriteToAsync
                Assert.ThrowsAsync <ArgumentNullException> (async() => await list.WriteToAsync(FormatOptions.Default, null));
                Assert.ThrowsAsync <ArgumentNullException> (async() => await list.WriteToAsync(null, stream));
                Assert.ThrowsAsync <ArgumentNullException> (async() => await list.WriteToAsync(null));
            }

            // Indexers
            Assert.Throws <ArgumentOutOfRangeException> (() => list[-1] = new Header(HeaderId.AdHoc, "value"));
            Assert.Throws <ArgumentOutOfRangeException> (() => list[HeaderId.Unknown] = "value");
            Assert.Throws <ArgumentOutOfRangeException> (() => value          = list[HeaderId.Unknown]);
            Assert.Throws <ArgumentOutOfRangeException> (() => header         = list[-1]);
            Assert.Throws <ArgumentNullException> (() => list[HeaderId.AdHoc] = null);
            Assert.Throws <ArgumentNullException> (() => value         = list[null]);
            Assert.Throws <ArgumentNullException> (() => list[null]    = "value");
            Assert.Throws <ArgumentNullException> (() => list["field"] = null);
            Assert.Throws <ArgumentNullException> (() => list[0]       = null);
        }
Exemple #3
0
		public void TestArgumentExceptions ()
		{
			var list = new HeaderList ();
			Header header;
			string value;

			// Add
			Assert.Throws<ArgumentNullException> (() => list.Add (null));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Add (HeaderId.Unknown, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Add (HeaderId.AdHoc, null));
			Assert.Throws<ArgumentNullException> (() => list.Add (null, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Add ("field", null));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Add (HeaderId.Unknown, Encoding.UTF8, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Add (HeaderId.AdHoc, null, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Add (HeaderId.AdHoc, Encoding.UTF8, null));
			Assert.Throws<ArgumentNullException> (() => list.Add (null, Encoding.UTF8, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Add ("field", null, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Add ("field", Encoding.UTF8, null));

			// Contains
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Contains (HeaderId.Unknown));
			Assert.Throws<ArgumentNullException> (() => list.Contains ((Header) null));
			Assert.Throws<ArgumentNullException> (() => list.Contains ((string) null));

			// CopyTo
			Assert.Throws<ArgumentOutOfRangeException> (() => list.CopyTo (new Header[0], -1));
			Assert.Throws<ArgumentNullException> (() => list.CopyTo (null, 0));

			// IndexOf
			Assert.Throws<ArgumentOutOfRangeException> (() => list.IndexOf (HeaderId.Unknown));
			Assert.Throws<ArgumentNullException> (() => list.IndexOf ((Header) null));
			Assert.Throws<ArgumentNullException> (() => list.IndexOf ((string) null));

			// Insert
			list.Add ("field", "value");
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, new Header (HeaderId.AdHoc, "value")));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, HeaderId.AdHoc, Encoding.UTF8, "value"));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, "field", Encoding.UTF8, "value"));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, HeaderId.AdHoc, "value"));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, "field", "value"));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (0, HeaderId.Unknown, Encoding.UTF8, "value"));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (0, HeaderId.Unknown, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Insert (0, HeaderId.AdHoc, Encoding.UTF8, null));
			Assert.Throws<ArgumentNullException> (() => list.Insert (0, HeaderId.AdHoc, null, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Insert (0, HeaderId.AdHoc, null));
			Assert.Throws<ArgumentNullException> (() => list.Insert (0, null, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Insert (0, "field", null));
			Assert.Throws<ArgumentNullException> (() => list.Insert (0, null));

			// LastIndexOf
			Assert.Throws<ArgumentOutOfRangeException> (() => list.LastIndexOf (HeaderId.Unknown));
			Assert.Throws<ArgumentNullException> (() => list.LastIndexOf ((string) null));

			// Remove
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Remove (HeaderId.Unknown));
			Assert.Throws<ArgumentNullException> (() => list.Remove ((Header) null));
			Assert.Throws<ArgumentNullException> (() => list.Remove ((string) null));

			// RemoveAll
			Assert.Throws<ArgumentOutOfRangeException> (() => list.RemoveAll (HeaderId.Unknown));
			Assert.Throws<ArgumentNullException> (() => list.RemoveAll ((string) null));

			// RemoveAt
			Assert.Throws<ArgumentOutOfRangeException> (() => list.RemoveAt (-1));

			// Replace
			Assert.Throws<ArgumentNullException> (() => list.Replace (null));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Replace (HeaderId.Unknown, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Replace (HeaderId.AdHoc, null));
			Assert.Throws<ArgumentNullException> (() => list.Replace (null, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Replace ("field", null));
			Assert.Throws<ArgumentOutOfRangeException> (() => list.Replace (HeaderId.Unknown, Encoding.UTF8, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Replace (HeaderId.AdHoc, null, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Replace (HeaderId.AdHoc, Encoding.UTF8, null));
			Assert.Throws<ArgumentNullException> (() => list.Replace (null, Encoding.UTF8, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Replace ("field", null, "value"));
			Assert.Throws<ArgumentNullException> (() => list.Replace ("field", Encoding.UTF8, null));

			using (var stream = new MemoryStream ()) {
				// Load
				Assert.Throws<ArgumentNullException> (() => HeaderList.Load (ParserOptions.Default, (Stream) null));
				Assert.Throws<ArgumentNullException> (() => HeaderList.Load (ParserOptions.Default, (string) null));
				Assert.Throws<ArgumentNullException> (() => HeaderList.Load (null, stream));
				Assert.Throws<ArgumentNullException> (() => HeaderList.Load ((Stream) null));
				Assert.Throws<ArgumentNullException> (() => HeaderList.Load ((string) null));

				// WriteTo
				Assert.Throws<ArgumentNullException> (() => list.WriteTo (FormatOptions.Default, null));
				Assert.Throws<ArgumentNullException> (() => list.WriteTo (null, stream));
				Assert.Throws<ArgumentNullException> (() => list.WriteTo (null));
			}

			// Indexers
			Assert.Throws<ArgumentOutOfRangeException> (() => list[-1] = new Header (HeaderId.AdHoc, "value"));
			Assert.Throws<ArgumentOutOfRangeException> (() => list[HeaderId.Unknown] = "value");
			Assert.Throws<ArgumentOutOfRangeException> (() => value = list[HeaderId.Unknown]);
			Assert.Throws<ArgumentOutOfRangeException> (() => header = list[-1]);
			Assert.Throws<ArgumentNullException> (() => list[HeaderId.AdHoc] = null);
			Assert.Throws<ArgumentNullException> (() => value = list[null]);
			Assert.Throws<ArgumentNullException> (() => list[null] = "value");
			Assert.Throws<ArgumentNullException> (() => list["field"] = null);
			Assert.Throws<ArgumentNullException> (() => list[0] = null);
		}
Exemple #4
0
        public static void Convert(Stream source, Stream destination)
        {
            int i = 0;

            byte[][] array  = null;
            byte[]   array2 = null;
            using (Stream stream = new SuppressCloseStream(source))
            {
                using (MimeReader mimeReader = new MimeReader(stream, true, DecodingOptions.Default, MimeLimits.Unlimited, true, false))
                {
                    while (mimeReader.ReadNextPart())
                    {
                        while (i >= mimeReader.Depth)
                        {
                            byte[] array3 = array[--i];
                            if (array3 != null)
                            {
                                destination.Write(array3, 0, array3.Length - 2);
                                destination.Write(MimeString.TwoDashesCRLF, 0, MimeString.TwoDashesCRLF.Length);
                            }
                        }
                        if (i > 0)
                        {
                            byte[] array3 = array[i - 1];
                            if (array3 != null)
                            {
                                destination.Write(array3, 0, array3.Length);
                            }
                        }
                        HeaderList        headerList        = HeaderList.ReadFrom(mimeReader);
                        ContentTypeHeader contentTypeHeader = headerList.FindFirst(HeaderId.ContentType) as ContentTypeHeader;
                        bool flag;
                        bool flag2;
                        bool flag3;
                        EightToSevenBitConverter.Analyse(contentTypeHeader, out flag, out flag2, out flag3);
                        Header header = headerList.FindFirst(HeaderId.ContentTransferEncoding);
                        if (flag2 || flag)
                        {
                            if (header != null)
                            {
                                headerList.RemoveChild(header);
                            }
                            headerList.WriteTo(destination);
                            byte[] array3;
                            if (flag)
                            {
                                array3 = null;
                                destination.Write(MimeString.CrLf, 0, MimeString.CrLf.Length);
                            }
                            else
                            {
                                array3 = MimePart.GetBoundary(contentTypeHeader);
                            }
                            if (array == null)
                            {
                                array = new byte[4][];
                            }
                            else if (array.Length == i)
                            {
                                byte[][] array4 = new byte[array.Length * 2][];
                                Array.Copy(array, 0, array4, 0, i);
                                array = array4;
                            }
                            array[i++] = array3;
                        }
                        else
                        {
                            Stream stream2 = null;
                            try
                            {
                                stream2 = mimeReader.GetRawContentReadStream();
                                if (header != null && stream2 != null)
                                {
                                    ContentTransferEncoding encodingType = MimePart.GetEncodingType(header.FirstRawToken);
                                    if (encodingType == ContentTransferEncoding.EightBit || encodingType == ContentTransferEncoding.Binary)
                                    {
                                        if (flag3)
                                        {
                                            header.RawValue = MimeString.QuotedPrintable;
                                            stream2         = new EncoderStream(stream2, new QPEncoder(), EncoderStreamAccess.Read);
                                        }
                                        else
                                        {
                                            header.RawValue = MimeString.Base64;
                                            stream2         = new EncoderStream(stream2, new Base64Encoder(), EncoderStreamAccess.Read);
                                        }
                                    }
                                }
                                headerList.WriteTo(destination);
                                destination.Write(MimeString.CrLf, 0, MimeString.CrLf.Length);
                                if (stream2 != null)
                                {
                                    DataStorage.CopyStreamToStream(stream2, destination, long.MaxValue, ref array2);
                                }
                            }
                            finally
                            {
                                if (stream2 != null)
                                {
                                    stream2.Dispose();
                                }
                            }
                        }
                    }
                    while (i > 0)
                    {
                        byte[] array3 = array[--i];
                        if (array3 != null)
                        {
                            destination.Write(array3, 0, array3.Length - 2);
                            destination.Write(MimeString.TwoDashesCRLF, 0, MimeString.TwoDashesCRLF.Length);
                        }
                    }
                }
            }
        }