예제 #1
0
        internal void WritePartWithHeaders(MimePart part, bool copyBoundaryToSkeleton)
        {
            this.StartPart(null, copyBoundaryToSkeleton);
            if ((this.flags & MimeStreamWriter.Flags.SkipHeaders) == MimeStreamWriter.Flags.SkipHeaders && this.mimeTextStream != null)
            {
                this.mimeTextStream.StartWriting();
                this.flags &= (MimeStreamWriter.Flags)(-2);
                if (this.mimeWriter == null)
                {
                    goto IL_98;
                }
                using (Stream rawContentWriteStream = this.mimeWriter.GetRawContentWriteStream())
                {
                    using (Stream rawContentReadStream = part.GetRawContentReadStream())
                    {
                        Util.StreamHandler.CopyStreamData(rawContentReadStream, rawContentWriteStream);
                    }
                    goto IL_98;
                }
            }
            if (this.mimeWriter != null)
            {
                using (Stream rawContentWriteStream2 = this.mimeWriter.GetRawContentWriteStream())
                {
                    part.WriteTo(rawContentWriteStream2);
                }
            }
IL_98:
            this.EndPart(copyBoundaryToSkeleton);
        }
예제 #2
0
        internal void WriteMimeNode(MimeNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            Header header = node as Header;

            if (header != null)
            {
                this.WriteHeader(header);
                this.FlushHeader();
                return;
            }
            MimePart mimePart = node as MimePart;

            if (mimePart != null)
            {
                this.StartPart();
                mimePart.WriteTo(this.shimStream, this.encodingOptions);
                this.EndPart();
                return;
            }
            HeaderList headerList = node as HeaderList;

            if (headerList != null)
            {
                foreach (Header header2 in headerList)
                {
                    this.WriteHeader(header);
                }
                this.FlushHeader();
                return;
            }
            node = node.Clone();
            MimeRecipient mimeRecipient = node as MimeRecipient;

            if (mimeRecipient != null)
            {
                this.WriteRecipient(mimeRecipient);
                return;
            }
            MimeParameter mimeParameter = node as MimeParameter;

            if (mimeParameter != null)
            {
                this.WriteParameter(mimeParameter);
                return;
            }
            MimeGroup mimeGroup = node as MimeGroup;

            if (mimeGroup != null)
            {
                this.StartGroup(mimeGroup);
                this.EndGroup();
            }
        }
예제 #3
0
        public void TestTranscoding()
        {
            var path     = Path.Combine("..", "..", "TestData", "images", "girl.jpg");
            var expected = File.ReadAllBytes(path);

            var part = new MimePart("image", "jpeg")
            {
                Content = new MimeContent(new MemoryStream(expected, false)),
                ContentTransferEncoding = ContentEncoding.Base64,
                FileName = "girl.jpg"
            };

            // encode in base64
            using (var output = new MemoryStream()) {
                part.WriteTo(output);
                output.Position = 0;

                part = (MimePart)MimeEntity.Load(output);
            }

            // transcode to uuencode
            part.ContentTransferEncoding = ContentEncoding.UUEncode;
            using (var output = new MemoryStream()) {
                part.WriteTo(output);
                output.Position = 0;

                part = (MimePart)MimeEntity.Load(output);
            }

            // verify decoded content
            using (var output = new MemoryStream()) {
                part.Content.DecodeTo(output);
                output.Position = 0;

                var actual = output.ToArray();

                Assert.AreEqual(expected.Length, actual.Length);
                for (int i = 0; i < expected.Length; i++)
                {
                    Assert.AreEqual(expected[i], actual[i], "Image content differs at index {0}", i);
                }
            }
        }
예제 #4
0
        public void TestParameterizedCtor()
        {
            const string expected = "Content-Type: text/plain\nContent-Transfer-Encoding: base64\nContent-Id: <*****@*****.**>\n\n";
            var          headers  = new [] { new Header("Content-Id", "<*****@*****.**>") };
            var          part     = new MimePart("text", "plain", new Header("Content-Transfer-Encoding", "base64"), headers)
            {
                Content = new MimeContent(new MemoryStream())
            };

            Assert.AreEqual("*****@*****.**", part.ContentId, "Content-Id");
            Assert.AreEqual(ContentEncoding.Base64, part.ContentTransferEncoding, "Content-Transfer-Encoding");

            using (var stream = new MemoryStream()) {
                var options = FormatOptions.Default.Clone();
                options.NewLineFormat = NewLineFormat.Unix;

                part.WriteTo(options, stream);

                var serialized = Encoding.ASCII.GetString(stream.GetBuffer(), 0, (int)stream.Length);
                Assert.AreEqual(expected, serialized, "Serialized");
            }
        }
예제 #5
0
        public void TestArgumentExceptions()
        {
            var part = new MimePart();

            Assert.Throws <ArgumentNullException> (() => new MimePart((string)null));
            Assert.Throws <ArgumentNullException> (() => new MimePart((ContentType)null));
            Assert.Throws <ArgumentNullException> (() => new MimePart(null, "octet-stream"));
            Assert.Throws <ArgumentNullException> (() => new MimePart("application", null));

            Assert.Throws <ArgumentOutOfRangeException> (() => part.ContentDuration = -1);
            Assert.Throws <ArgumentOutOfRangeException> (() => part.Prepare(EncodingConstraint.SevenBit, 1));

            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load((Stream)null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load((Stream)null, true));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load((ParserOptions)null, Stream.Null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(ParserOptions.Default, (Stream)null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(null, Stream.Null, true));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(ParserOptions.Default, (Stream)null, true));

            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load((ContentType)null, Stream.Null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(new ContentType("application", "octet-stream"), null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(null, new ContentType("application", "octet-stream"), Stream.Null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(ParserOptions.Default, null, Stream.Null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(ParserOptions.Default, new ContentType("application", "octet-stream"), null));

            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load((string)null));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(null, "fileName"));
            Assert.Throws <ArgumentNullException> (() => MimeEntity.Load(ParserOptions.Default, (string)null));

            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync((Stream)null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync((Stream)null, true));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync((ParserOptions)null, Stream.Null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(ParserOptions.Default, (Stream)null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(null, Stream.Null, true));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(ParserOptions.Default, (Stream)null, true));

            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync((ContentType)null, Stream.Null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(new ContentType("application", "octet-stream"), null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(null, new ContentType("application", "octet-stream"), Stream.Null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(ParserOptions.Default, null, Stream.Null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(ParserOptions.Default, new ContentType("application", "octet-stream"), null));

            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync((string)null));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(null, "fileName"));
            Assert.Throws <ArgumentNullException> (async() => await MimeEntity.LoadAsync(ParserOptions.Default, (string)null));

            Assert.Throws <ArgumentNullException> (() => part.Accept(null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo((string)null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo((Stream)null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo((string)null, false));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo((Stream)null, false));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(null, Stream.Null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(FormatOptions.Default, (Stream)null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(null, "fileName"));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(FormatOptions.Default, (string)null));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(null, Stream.Null, false));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(FormatOptions.Default, (Stream)null, false));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(null, "fileName", false));
            Assert.Throws <ArgumentNullException> (() => part.WriteTo(FormatOptions.Default, (string)null, false));
            Assert.Throws <ArgumentException> (() => part.ContentId = "this is some text and stuff");

            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync((string)null));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync((Stream)null));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync((string)null, false));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync((Stream)null, false));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(null, Stream.Null));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(FormatOptions.Default, (Stream)null));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(null, "fileName"));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(FormatOptions.Default, (string)null));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(null, Stream.Null, false));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(FormatOptions.Default, (Stream)null, false));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(null, "fileName", false));
            Assert.Throws <ArgumentNullException> (async() => await part.WriteToAsync(FormatOptions.Default, (string)null, false));
        }
 public static string WriteToString(this MimePart? mimePart, Encoding encoding)
 {
     using var stream = new MemoryStream();
     mimePart?.WriteTo(stream);
     return encoding.GetString(stream.ToArray());
 }
예제 #7
0
        public static void  DecryptMessage(string path, WebBrowser wb, ListView attachments, ImageList im)
        {
            FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);

            try
            {
                MimeParser b = new MimeParser(f);


                var    mime = b.ParseMessage();
                string s    = mime.HtmlBody;
                if (mime.HtmlBody == null)
                {
                    s = mime.TextBody;
                }
                wb.DocumentText = s;
                attachments.Clear();
                //Рас
                var    Attach     = mime.Attachments;
                string AttachPath = path.Substring(0, path.LastIndexOf('.')) + "_attachments";

                if (Attach.Count <MimeEntity>() > 0)
                {
                    Directory.CreateDirectory(AttachPath);
                    var dir = Directory.EnumerateFiles(AttachPath);
                    foreach (MimeEntity m in Attach)
                    {
                        var          t        = m.Headers;
                        ListViewItem viewItem = new ListViewItem(m.ContentDisposition.FileName);
                        string       tmp      = Path.GetExtension(m.ContentDisposition.FileName).Replace(".", "");
                        if (im.Images.ContainsKey(tmp + ".png"))
                        {
                            viewItem.ImageKey = tmp + ".png";
                        }
                        else
                        {
                            viewItem.ImageKey = "blank.png";
                        }

                        attachments.Items.Add(viewItem);

                        if (!dir.Contains <string>(AttachPath + "\\" + m.ContentDisposition.FileName))
                        {
                            FileStream attach = new FileStream(AttachPath + "\\" + m.ContentDisposition.FileName, FileMode.Create, FileAccess.Write);
                            try {
                                MimePart m1 = m as MimePart;
                                m1.ContentTransferEncoding = ContentEncoding.Binary;
                                m1.WriteTo(FormatOptions.Default, attach, true);
                            }
                            finally {
                                attach.Close();
                            }
                        }
                    }
                }
            }
            catch (Exception ex) {
            }
            finally
            {
                f.Close();
            }
        }