/// <summary>
 /// Initializes a new instance of the <see cref="AttachedFile"/> class.
 /// </summary>
 /// <param name="part">A multipart MIME part containing the attached file.</param>
 public AttachedFile(MimePart part)
 {
     Guard.ArgumentNotNull(part, "part");
     _contentType = part.ContentType;
     _fileName = GetFileName(part.ContentDisposition);
     _stream = part.Content;
 }
        public void Write_Single_Plain_Text_Test()
        {
            var part = new MimePart(new MemoryStream(Encoding.UTF8.GetBytes("plain text")));
            string result;

            using (var message = new MimeMessage(part) {Boundary = "abc123"})
            using (var stream = new MemoryStream())
            using (var reader = new StreamReader(stream))
            {
                message.WriteTo(stream);
                stream.Seek(0, SeekOrigin.Begin);
                result = reader.ReadToEnd();
            }

            const string expected = "--abc123\r\n" +
                                    "Content-Length: 10\r\n" +
                                    "\r\n" +
                                    "plain text\r\n" +
                                    "--abc123--";
            Assert.That(result, Is.EqualTo(expected));
        }
        public void Write_Headers_Test()
        {
            string result;
            var part = new MimePart(null)
                           {
                               ContentType = "plain/xml",
                               ContentLength = 20,
                               ContentTransferEncoding = "binary",
                               ContentDisposition = "attachment"
                           };

            var message = new MimeMessage(part) {Boundary = "abc123"};

            using (var stream = new MemoryStream())
            using (var reader = new StreamReader(stream))
            {
                message.WriteTo(stream);
                stream.Seek(0, SeekOrigin.Begin);
                result = reader.ReadToEnd();
            }

            const string expected = "--abc123\r\n" +
                                    "Content-Type: plain/xml\r\n" +
                                    "Content-Length: 20\r\n" +
                                    "Content-Transfer-Encoding: binary\r\n" +
                                    "Content-Disposition: attachment\r\n" +
                                    "\r\n" +
                                    "\r\n" +
                                    "--abc123--";
            Assert.That(result, Is.EqualTo(expected));
        }
        public void Write_Binary_Test()
        {
            var bytes = Enumerable.Range(0, 256).Select(i => (byte) i).ToArray();
            var part = new MimePart(new MemoryStream(bytes));
            string result;

            using (var message = new MimeMessage(part) {Boundary = "abc123"})
            using (var stream = new MemoryStream())
            using (var reader = new StreamReader(stream))
            {
                message.WriteTo(stream);
                stream.Seek(0, SeekOrigin.Begin);
                result = reader.ReadToEnd();
            }

            var expected = "--abc123\r\n" +
                           "Content-Length: 256\r\n" +
                           "\r\n" +
                           Encoding.UTF8.GetString(bytes) + "\r\n" +
                           "--abc123--";
            Assert.That(result, Is.EqualTo(expected));
        }