Ejemplo n.º 1
0
        static void LoadContent(MimePart attachment, Stream stream)
        {
            var content = new MemoryBlockStream();

            if (attachment.ContentType.IsMimeType("text", "*"))
            {
                var filter = new BestEncodingFilter();
                var buf = new byte[4096];
                int index, length;
                int nread;

                while ((nread = stream.Read(buf, 0, buf.Length)) > 0)
                {
                    filter.Filter(buf, 0, nread, out index, out length);
                    content.Write(buf, 0, nread);
                }

                filter.Flush(buf, 0, 0, out index, out length);

                attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
            }
            else
            {
                attachment.ContentTransferEncoding = ContentEncoding.Base64;
                stream.CopyTo(content, 4096);
            }

            content.Position = 0;

            attachment.Content = new MimeContent(content);
        }
Ejemplo n.º 2
0
        void LoadContent(MimePart attachment, string fileName)
        {
            var content = new MemoryBlockStream();
            var filter  = new BestEncodingFilter();

            using (var stream = File.OpenRead(fileName)) {
                var buf = new byte[4096];
                int index, length;
                int nread;

                while ((nread = stream.Read(buf, 0, buf.Length)) > 0)
                {
                    filter.Filter(buf, 0, nread, out index, out length);
                    content.Write(buf, 0, nread);
                }

                filter.Flush(buf, 0, 0, out index, out length);
            }

            content.Position = 0;

            if (linked)
            {
                attachment.ContentLocation = new Uri(Path.GetFileName(fileName), UriKind.Relative);
            }

            attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
            attachment.ContentObject           = new ContentObject(content, ContentEncoding.Default);
        }
Ejemplo n.º 3
0
        private bool StoreEntities(OutgoingEmail email, List <string> attachmentList)
        {
            foreach (string iter in attachmentList)
            {
                FileStream stream = File.OpenRead(iter);
                if (!stream.CanRead)
                {
                    return(false);
                }

                string      mimeType = MimeTypes.GetMimeType(iter);
                ContentType fileType = ContentType.Parse(mimeType);
                MimePart    attachment;
                if (fileType.IsMimeType("text", "*"))
                {
                    attachment = new TextPart(fileType.MediaSubtype);
                    foreach (var param in fileType.Parameters)
                    {
                        attachment.ContentType.Parameters.Add(param);
                    }
                }
                else
                {
                    attachment = new MimePart(fileType);
                }
                attachment.FileName     = Path.GetFileName(iter);
                attachment.IsAttachment = true;

                MemoryBlockStream  memoryBlockStream = new MemoryBlockStream();
                BestEncodingFilter encodingFilter = new BestEncodingFilter();
                byte[]             fileBuffer = new byte[4096];
                int index, length, bytesRead;

                while ((bytesRead = stream.Read(fileBuffer, 0, fileBuffer.Length)) > 0)
                {
                    encodingFilter.Filter(fileBuffer, 0, bytesRead, out index, out length);
                    memoryBlockStream.Write(fileBuffer, 0, bytesRead);
                }

                encodingFilter.Flush(fileBuffer, 0, 0, out index, out length);
                memoryBlockStream.Position = 0;

                attachment.ContentTransferEncoding = encodingFilter.GetBestEncoding(EncodingConstraint.SevenBit);
                attachment.ContentObject           = new ContentObject(memoryBlockStream);

                if (attachment != null)
                {
                    email.AttachmentList.Add(attachment);
                }
            }
            return(true);
        }
Ejemplo n.º 4
0
        static void LoadContent(MimePart attachment, Stream stream)
        {
            var content = new MemoryBlockStream();
            var filter = new BestEncodingFilter();
            var buf = new byte[4096];
            int index, length;
            int nread;

            while ((nread = stream.Read(buf, 0, buf.Length)) > 0)
            {
                filter.Filter(buf, 0, nread, out index, out length);
                content.Write(buf, 0, nread);
            }

            filter.Flush(buf, 0, 0, out index, out length);
            content.Position = 0;

            attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
            attachment.ContentObject           = new ContentObject(content);
        }
Ejemplo n.º 5
0
        static async Task LoadContentAsync(MimePart attachment, Stream stream, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var content = new MemoryBlockStream();

            if (attachment.ContentType.IsMimeType("text", "*"))
            {
                var buf = ArrayPool <byte> .Shared.Rent(BufferLength);

                var filter = new BestEncodingFilter();
                int index, length;
                int nread;

                try {
                    while ((nread = await stream.ReadAsync(buf, 0, BufferLength, cancellationToken).ConfigureAwait(false)) > 0)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        filter.Filter(buf, 0, nread, out index, out length);
                        content.Write(buf, 0, nread);
                    }

                    filter.Flush(buf, 0, 0, out index, out length);
                } finally {
                    ArrayPool <byte> .Shared.Return(buf);
                }

                attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
            }
            else
            {
                attachment.ContentTransferEncoding = ContentEncoding.Base64;
                await stream.CopyToAsync(content, 4096, cancellationToken).ConfigureAwait(false);
            }

            content.Position = 0;

            attachment.Content = new MimeContent(content);
        }