Exemple #1
1
        static void ExtractAttachments(TnefReader reader, BodyBuilder builder)
        {
            var      filter = new BestEncodingFilter();
            var      prop = reader.TnefPropertyReader;
            MimePart attachment = null;
            int      outIndex, outLength;

            byte[] attachData;
            string text;

            do
            {
                if (reader.AttributeLevel != TnefAttributeLevel.Attachment)
                {
                    break;
                }

                switch (reader.AttributeTag)
                {
                case TnefAttributeTag.AttachRenderData:
                    if (attachment != null)
                    {
                        builder.Attachments.Add(attachment);
                    }

                    attachment = new MimePart();
                    break;

                case TnefAttributeTag.Attachment:
                    if (attachment == null)
                    {
                        break;
                    }

                    while (prop.ReadNextProperty())
                    {
                        switch (prop.PropertyTag.Id)
                        {
                        case TnefPropertyId.AttachLongFilename:
                            attachment.FileName = prop.ReadValueAsString();
                            break;

                        case TnefPropertyId.AttachFilename:
                            if (attachment.FileName == null)
                            {
                                attachment.FileName = prop.ReadValueAsString();
                            }
                            break;

                        case TnefPropertyId.AttachContentLocation:
                            text = prop.ReadValueAsString();
                            if (Uri.IsWellFormedUriString(text, UriKind.Absolute))
                            {
                                attachment.ContentLocation = new Uri(text, UriKind.Absolute);
                            }
                            else if (Uri.IsWellFormedUriString(text, UriKind.Relative))
                            {
                                attachment.ContentLocation = new Uri(text, UriKind.Relative);
                            }
                            break;

                        case TnefPropertyId.AttachContentBase:
                            text = prop.ReadValueAsString();
                            attachment.ContentBase = new Uri(text, UriKind.Absolute);
                            break;

                        case TnefPropertyId.AttachContentId:
                            attachment.ContentId = prop.ReadValueAsString();
                            break;

                        case TnefPropertyId.AttachDisposition:
                            text = prop.ReadValueAsString();
                            if (attachment.ContentDisposition == null)
                            {
                                attachment.ContentDisposition = new ContentDisposition(text);
                            }
                            else
                            {
                                attachment.ContentDisposition.Disposition = text;
                            }
                            break;

                        case TnefPropertyId.AttachData:
                            // TODO: implement this...
                            break;
                        }
                    }
                    break;

                case TnefAttributeTag.AttachData:
                    if (attachment == null)
                    {
                        break;
                    }

                    attachData = prop.ReadValueAsBytes();
                    filter.Flush(attachData, 0, attachData.Length, out outIndex, out outLength);
                    attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.EightBit);
                    attachment.ContentObject           = new ContentObject(new MemoryStream(attachData, false), ContentEncoding.Default);
                    filter.Reset();
                    break;
                }

                if (attachment != null)
                {
                    builder.Attachments.Add(attachment);
                }
            } while (reader.ReadNextAttribute());
        }
Exemple #2
1
        static void ExtractAttachments(TnefReader reader, BodyBuilder builder)
        {
            var             attachMethod = TnefAttachMethod.ByValue;
            var             filter = new BestEncodingFilter();
            var             prop = reader.TnefPropertyReader;
            MimePart        attachment = null;
            int             outIndex, outLength;
            TnefAttachFlags flags;

            string[] mimeType;
            byte[]   attachData;
            string   text;

            do
            {
                if (reader.AttributeLevel != TnefAttributeLevel.Attachment)
                {
                    break;
                }

                switch (reader.AttributeTag)
                {
                case TnefAttributeTag.AttachRenderData:
                    attachMethod = TnefAttachMethod.ByValue;
                    attachment   = new MimePart();
                    break;

                case TnefAttributeTag.Attachment:
                    if (attachment == null)
                    {
                        break;
                    }

                    attachData = null;

                    while (prop.ReadNextProperty())
                    {
                        switch (prop.PropertyTag.Id)
                        {
                        case TnefPropertyId.AttachLongFilename:
                            attachment.FileName = prop.ReadValueAsString();
                            break;

                        case TnefPropertyId.AttachFilename:
                            if (attachment.FileName == null)
                            {
                                attachment.FileName = prop.ReadValueAsString();
                            }
                            break;

                        case TnefPropertyId.AttachContentLocation:
                            attachment.ContentLocation = prop.ReadValueAsUri();
                            break;

                        case TnefPropertyId.AttachContentBase:
                            attachment.ContentBase = prop.ReadValueAsUri();
                            break;

                        case TnefPropertyId.AttachContentId:
                            text = prop.ReadValueAsString();

                            var buffer = CharsetUtils.UTF8.GetBytes(text);
                            int index  = 0;

                            if (ParseUtils.TryParseMsgId(buffer, ref index, buffer.Length, false, false, out string msgid))
                            {
                                attachment.ContentId = msgid;
                            }
                            break;

                        case TnefPropertyId.AttachDisposition:
                            text = prop.ReadValueAsString();
                            if (ContentDisposition.TryParse(text, out ContentDisposition disposition))
                            {
                                attachment.ContentDisposition = disposition;
                            }
                            break;

                        case TnefPropertyId.AttachData:
                            attachData = prop.ReadValueAsBytes();
                            break;

                        case TnefPropertyId.AttachMethod:
                            attachMethod = (TnefAttachMethod)prop.ReadValueAsInt32();
                            break;

                        case TnefPropertyId.AttachMimeTag:
                            mimeType = prop.ReadValueAsString().Split('/');
                            if (mimeType.Length == 2)
                            {
                                attachment.ContentType.MediaType    = mimeType[0].Trim();
                                attachment.ContentType.MediaSubtype = mimeType[1].Trim();
                            }
                            break;

                        case TnefPropertyId.AttachFlags:
                            flags = (TnefAttachFlags)prop.ReadValueAsInt32();
                            if ((flags & TnefAttachFlags.RenderedInBody) != 0)
                            {
                                if (attachment.ContentDisposition == null)
                                {
                                    attachment.ContentDisposition = new ContentDisposition(ContentDisposition.Inline);
                                }
                                else
                                {
                                    attachment.ContentDisposition.Disposition = ContentDisposition.Inline;
                                }
                            }
                            break;

                        case TnefPropertyId.AttachSize:
                            if (attachment.ContentDisposition == null)
                            {
                                attachment.ContentDisposition = new ContentDisposition();
                            }

                            attachment.ContentDisposition.Size = prop.ReadValueAsInt64();
                            break;

                        case TnefPropertyId.DisplayName:
                            attachment.ContentType.Name = prop.ReadValueAsString();
                            break;
                        }
                    }

                    if (attachData != null)
                    {
                        int count = attachData.Length;
                        int index = 0;

                        if (attachMethod == TnefAttachMethod.EmbeddedMessage)
                        {
                            attachment.ContentTransferEncoding = ContentEncoding.Base64;
                            attachment = PromoteToTnefPart(attachment);
                            count     -= 16;
                            index      = 16;
                        }
                        else
                        {
                            filter.Flush(attachData, index, count, out outIndex, out outLength);
                            attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
                            filter.Reset();
                        }

                        attachment.Content = new MimeContent(new MemoryStream(attachData, index, count, false));
                        builder.Attachments.Add(attachment);
                    }
                    break;

                case TnefAttributeTag.AttachCreateDate:
                    if (attachment != null)
                    {
                        if (attachment.ContentDisposition == null)
                        {
                            attachment.ContentDisposition = new ContentDisposition();
                        }

                        attachment.ContentDisposition.CreationDate = prop.ReadValueAsDateTime();
                    }
                    break;

                case TnefAttributeTag.AttachModifyDate:
                    if (attachment != null)
                    {
                        if (attachment.ContentDisposition == null)
                        {
                            attachment.ContentDisposition = new ContentDisposition();
                        }

                        attachment.ContentDisposition.ModificationDate = prop.ReadValueAsDateTime();
                    }
                    break;

                case TnefAttributeTag.AttachTitle:
                    if (attachment != null && string.IsNullOrEmpty(attachment.FileName))
                    {
                        attachment.FileName = prop.ReadValueAsString();
                    }
                    break;

                case TnefAttributeTag.AttachMetaFile:
                    if (attachment == null)
                    {
                        break;
                    }

                    // TODO: what to do with the meta data?
                    break;

                case TnefAttributeTag.AttachData:
                    if (attachment == null || attachMethod != TnefAttachMethod.ByValue)
                    {
                        break;
                    }

                    attachData = prop.ReadValueAsBytes();
                    filter.Flush(attachData, 0, attachData.Length, out outIndex, out outLength);
                    attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
                    attachment.Content = new MimeContent(new MemoryStream(attachData, false));
                    filter.Reset();

                    builder.Attachments.Add(attachment);
                    break;
                }
            } while (reader.ReadNextAttribute());
        }
Exemple #3
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);
        }
        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);
        }
Exemple #5
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);
        }
        void LoadContent(MimePart attachment, string fileName, byte[] data)
        {
            var content = new MemoryBlockStream();
            var filter = new BestEncodingFilter();
            int index, length;

            filter.Flush(data, 0, data.Length, out index, out length);
            content.Write(data, 0, data.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);
        }
        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);
        }
        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);
        }
Exemple #9
0
        static void ExtractAttachments(TnefReader reader, BodyBuilder builder)
        {
            var             attachMethod = TnefAttachMethod.ByValue;
            var             filter = new BestEncodingFilter();
            var             prop = reader.TnefPropertyReader;
            MimePart        attachment = null;
            int             outIndex, outLength;
            TnefAttachFlags flags;

            string[] mimeType;
            byte[]   attachData;
            DateTime time;
            string   text;

            //Console.WriteLine ("Extracting attachments...");

            do
            {
                if (reader.AttributeLevel != TnefAttributeLevel.Attachment)
                {
                    Assert.Fail("Expected attachment attribute level: {0}", reader.AttributeLevel);
                }

                switch (reader.AttributeTag)
                {
                case TnefAttributeTag.AttachRenderData:
                    //Console.WriteLine ("Attachment Attribute: {0}", reader.AttributeTag);
                    attachMethod = TnefAttachMethod.ByValue;
                    attachment   = new MimePart();
                    break;

                case TnefAttributeTag.Attachment:
                    //Console.WriteLine ("Attachment Attribute: {0}", reader.AttributeTag);
                    if (attachment == null)
                    {
                        break;
                    }

                    while (prop.ReadNextProperty())
                    {
                        switch (prop.PropertyTag.Id)
                        {
                        case TnefPropertyId.AttachLongFilename:
                            attachment.FileName = prop.ReadValueAsString();

                            //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, attachment.FileName);
                            break;

                        case TnefPropertyId.AttachFilename:
                            if (attachment.FileName == null)
                            {
                                attachment.FileName = prop.ReadValueAsString();
                                //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, attachment.FileName);
                            }
                            else
                            {
                                //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, prop.ReadValueAsString ());
                            }
                            break;

                        case TnefPropertyId.AttachContentLocation:
                            text = prop.ReadValueAsString();
                            if (Uri.IsWellFormedUriString(text, UriKind.Absolute))
                            {
                                attachment.ContentLocation = new Uri(text, UriKind.Absolute);
                            }
                            else if (Uri.IsWellFormedUriString(text, UriKind.Relative))
                            {
                                attachment.ContentLocation = new Uri(text, UriKind.Relative);
                            }
                            //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, text);
                            break;

                        case TnefPropertyId.AttachContentBase:
                            text = prop.ReadValueAsString();
                            attachment.ContentBase = new Uri(text, UriKind.Absolute);
                            //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, text);
                            break;

                        case TnefPropertyId.AttachContentId:
                            attachment.ContentId = prop.ReadValueAsString();
                            //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, attachment.ContentId);
                            break;

                        case TnefPropertyId.AttachDisposition:
                            text = prop.ReadValueAsString();
                            if (attachment.ContentDisposition == null)
                            {
                                attachment.ContentDisposition = new ContentDisposition(text);
                            }
                            else
                            {
                                attachment.ContentDisposition.Disposition = text;
                            }
                            //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, text);
                            break;

                        case TnefPropertyId.AttachMethod:
                            attachMethod = (TnefAttachMethod)prop.ReadValueAsInt32();
                            //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, attachMethod);
                            break;

                        case TnefPropertyId.AttachMimeTag:
                            text     = prop.ReadValueAsString();
                            mimeType = text.Split('/');
                            if (mimeType.Length == 2)
                            {
                                attachment.ContentType.MediaType    = mimeType[0].Trim();
                                attachment.ContentType.MediaSubtype = mimeType[1].Trim();
                            }
                            //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, text);
                            break;

                        case TnefPropertyId.AttachFlags:
                            flags = (TnefAttachFlags)prop.ReadValueAsInt32();
                            if ((flags & TnefAttachFlags.RenderedInBody) != 0)
                            {
                                if (attachment.ContentDisposition == null)
                                {
                                    attachment.ContentDisposition = new ContentDisposition(ContentDisposition.Inline);
                                }
                                else
                                {
                                    attachment.ContentDisposition.Disposition = ContentDisposition.Inline;
                                }
                            }
                            //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, flags);
                            break;

                        case TnefPropertyId.AttachData:
                            var stream  = prop.GetRawValueReadStream();
                            var content = new MemoryStream();
                            var guid    = new byte[16];

                            if (attachMethod == TnefAttachMethod.EmbeddedMessage)
                            {
                                var tnef = new TnefPart();

                                foreach (var param in attachment.ContentType.Parameters)
                                {
                                    tnef.ContentType.Parameters[param.Name] = param.Value;
                                }

                                if (attachment.ContentDisposition != null)
                                {
                                    tnef.ContentDisposition = attachment.ContentDisposition;
                                }

                                attachment = tnef;
                            }

                            stream.Read(guid, 0, 16);

                            stream.CopyTo(content, 4096);

                            var buffer = content.GetBuffer();
                            filter.Flush(buffer, 0, (int)content.Length, out outIndex, out outLength);
                            attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
                            attachment.Content = new MimeContent(content);
                            filter.Reset();

                            //Console.WriteLine ("Attachment Property: {0} has GUID {1}", prop.PropertyTag.Id, new Guid (guid));

                            builder.Attachments.Add(attachment);
                            break;

                        case TnefPropertyId.DisplayName:
                            attachment.ContentType.Name = prop.ReadValueAsString();
                            //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, attachment.ContentType.Name);
                            break;

                        case TnefPropertyId.AttachSize:
                            if (attachment.ContentDisposition == null)
                            {
                                attachment.ContentDisposition = new ContentDisposition();
                            }

                            attachment.ContentDisposition.Size = prop.ReadValueAsInt64();
                            //Console.WriteLine ("Attachment Property: {0} = {1}", prop.PropertyTag.Id, attachment.ContentDisposition.Size.Value);
                            break;

                        default:
                            //Console.WriteLine ("Attachment Property (unhandled): {0} = {1}", prop.PropertyTag.Id, prop.ReadValue ());
                            break;
                        }
                    }
                    break;

                case TnefAttributeTag.AttachData:
                    //Console.WriteLine ("Attachment Attribute: {0}", reader.AttributeTag);
                    if (attachment == null || attachMethod != TnefAttachMethod.ByValue)
                    {
                        break;
                    }

                    attachData = prop.ReadValueAsBytes();
                    filter.Flush(attachData, 0, attachData.Length, out outIndex, out outLength);
                    attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
                    attachment.Content = new MimeContent(new MemoryStream(attachData, false));
                    filter.Reset();

                    builder.Attachments.Add(attachment);
                    break;

                case TnefAttributeTag.AttachCreateDate:
                    time = prop.ReadValueAsDateTime();

                    if (attachment != null)
                    {
                        if (attachment.ContentDisposition == null)
                        {
                            attachment.ContentDisposition = new ContentDisposition();
                        }

                        attachment.ContentDisposition.CreationDate = time;
                    }

                    //Console.WriteLine ("Attachment Attribute: {0} = {1}", reader.AttributeTag, time);
                    break;

                case TnefAttributeTag.AttachModifyDate:
                    time = prop.ReadValueAsDateTime();

                    if (attachment != null)
                    {
                        if (attachment.ContentDisposition == null)
                        {
                            attachment.ContentDisposition = new ContentDisposition();
                        }

                        attachment.ContentDisposition.ModificationDate = time;
                    }

                    //Console.WriteLine ("Attachment Attribute: {0} = {1}", reader.AttributeTag, time);
                    break;

                case TnefAttributeTag.AttachTitle:
                    text = prop.ReadValueAsString();

                    if (attachment != null && string.IsNullOrEmpty(attachment.FileName))
                    {
                        attachment.FileName = text;
                    }

                    //Console.WriteLine ("Attachment Attribute: {0} = {1}", reader.AttributeTag, text);
                    break;

                //case TnefAttributeTag.AttachMetaFile:
                //	break;
                default:
                    var type  = prop.ValueType;
                    var value = prop.ReadValue();
                    //Console.WriteLine ("Attachment Attribute (unhandled): {0} = {1}", reader.AttributeTag, value);
                    Assert.AreEqual(type, value.GetType(), "Unexpected value type for {0}: {1}", reader.AttributeTag, value.GetType().Name);
                    break;
                }
            } while (reader.ReadNextAttribute());
        }
Exemple #10
0
        static void ExtractAttachments(TnefReader reader, BodyBuilder builder)
        {
            var             attachMethod = TnefAttachMethod.ByValue;
            var             filter = new BestEncodingFilter();
            var             prop = reader.TnefPropertyReader;
            MimePart        attachment = null;
            int             outIndex, outLength;
            TnefAttachFlags flags;

            string[] mimeType;
            byte[]   attachData;
            string   text;

            do
            {
                if (reader.AttributeLevel != TnefAttributeLevel.Attachment)
                {
                    break;
                }

                switch (reader.AttributeTag)
                {
                case TnefAttributeTag.AttachRenderData:
                    attachMethod = TnefAttachMethod.ByValue;
                    attachment   = new MimePart();
                    break;

                case TnefAttributeTag.Attachment:
                    if (attachment == null)
                    {
                        break;
                    }

                    while (prop.ReadNextProperty())
                    {
                        switch (prop.PropertyTag.Id)
                        {
                        case TnefPropertyId.AttachLongFilename:
                            attachment.FileName = prop.ReadValueAsString();
                            break;

                        case TnefPropertyId.AttachFilename:
                            if (attachment.FileName == null)
                            {
                                attachment.FileName = prop.ReadValueAsString();
                            }
                            break;

                        case TnefPropertyId.AttachContentLocation:
                            text = prop.ReadValueAsString();
                            if (Uri.IsWellFormedUriString(text, UriKind.Absolute))
                            {
                                attachment.ContentLocation = new Uri(text, UriKind.Absolute);
                            }
                            else if (Uri.IsWellFormedUriString(text, UriKind.Relative))
                            {
                                attachment.ContentLocation = new Uri(text, UriKind.Relative);
                            }
                            break;

                        case TnefPropertyId.AttachContentBase:
                            text = prop.ReadValueAsString();
                            attachment.ContentBase = new Uri(text, UriKind.Absolute);
                            break;

                        case TnefPropertyId.AttachContentId:
                            attachment.ContentId = prop.ReadValueAsString();
                            break;

                        case TnefPropertyId.AttachDisposition:
                            text = prop.ReadValueAsString();
                            if (attachment.ContentDisposition == null)
                            {
                                attachment.ContentDisposition = new ContentDisposition(text);
                            }
                            else
                            {
                                attachment.ContentDisposition.Disposition = text;
                            }
                            break;

                        case TnefPropertyId.AttachData:
                            var stream  = prop.GetRawValueReadStream();
                            var content = new MemoryStream();
                            var guid    = new byte[16];

                            if (attachMethod == TnefAttachMethod.EmbeddedMessage)
                            {
                                var tnef = new TnefPart();

                                foreach (var param in attachment.ContentType.Parameters)
                                {
                                    tnef.ContentType.Parameters[param.Name] = param.Value;
                                }

                                if (attachment.ContentDisposition != null)
                                {
                                    tnef.ContentDisposition = attachment.ContentDisposition;
                                }

                                attachment = tnef;
                            }

                            // read the GUID
                            stream.Read(guid, 0, 16);

                            // the rest is content
                            using (var filtered = new FilteredStream(content)) {
                                filtered.Add(filter);
                                stream.CopyTo(filtered, 4096);
                                filtered.Flush();
                            }

                            content.Position = 0;

                            attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
                            attachment.Content = new MimeContent(content);
                            filter.Reset();

                            builder.Attachments.Add(attachment);
                            break;

                        case TnefPropertyId.AttachMethod:
                            attachMethod = (TnefAttachMethod)prop.ReadValueAsInt32();
                            break;

                        case TnefPropertyId.AttachMimeTag:
                            mimeType = prop.ReadValueAsString().Split('/');
                            if (mimeType.Length == 2)
                            {
                                attachment.ContentType.MediaType    = mimeType[0].Trim();
                                attachment.ContentType.MediaSubtype = mimeType[1].Trim();
                            }
                            break;

                        case TnefPropertyId.AttachFlags:
                            flags = (TnefAttachFlags)prop.ReadValueAsInt32();
                            if ((flags & TnefAttachFlags.RenderedInBody) != 0)
                            {
                                if (attachment.ContentDisposition == null)
                                {
                                    attachment.ContentDisposition = new ContentDisposition(ContentDisposition.Inline);
                                }
                                else
                                {
                                    attachment.ContentDisposition.Disposition = ContentDisposition.Inline;
                                }
                            }
                            break;

                        case TnefPropertyId.AttachSize:
                            if (attachment.ContentDisposition == null)
                            {
                                attachment.ContentDisposition = new ContentDisposition();
                            }

                            attachment.ContentDisposition.Size = prop.ReadValueAsInt64();
                            break;

                        case TnefPropertyId.DisplayName:
                            attachment.ContentType.Name = prop.ReadValueAsString();
                            break;
                        }
                    }
                    break;

                case TnefAttributeTag.AttachCreateDate:
                    if (attachment != null)
                    {
                        if (attachment.ContentDisposition == null)
                        {
                            attachment.ContentDisposition = new ContentDisposition();
                        }

                        attachment.ContentDisposition.CreationDate = prop.ReadValueAsDateTime();
                    }
                    break;

                case TnefAttributeTag.AttachModifyDate:
                    if (attachment != null)
                    {
                        if (attachment.ContentDisposition == null)
                        {
                            attachment.ContentDisposition = new ContentDisposition();
                        }

                        attachment.ContentDisposition.ModificationDate = prop.ReadValueAsDateTime();
                    }
                    break;

                case TnefAttributeTag.AttachTitle:
                    if (attachment != null && string.IsNullOrEmpty(attachment.FileName))
                    {
                        attachment.FileName = prop.ReadValueAsString();
                    }
                    break;

                case TnefAttributeTag.AttachMetaFile:
                    if (attachment == null)
                    {
                        break;
                    }

                    // TODO: what to do with the meta data?
                    break;

                case TnefAttributeTag.AttachData:
                    if (attachment == null || attachMethod != TnefAttachMethod.ByValue)
                    {
                        break;
                    }

                    attachData = prop.ReadValueAsBytes();
                    filter.Flush(attachData, 0, attachData.Length, out outIndex, out outLength);
                    attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.EightBit);
                    attachment.Content = new MimeContent(new MemoryStream(attachData, false));
                    filter.Reset();

                    builder.Attachments.Add(attachment);
                    break;
                }
            } while (reader.ReadNextAttribute());
        }
Exemple #11
0
        static void ExtractAttachments(TnefReader reader, BodyBuilder builder)
        {
            var      filter = new BestEncodingFilter();
            var      prop = reader.TnefPropertyReader;
            MimePart attachment = null;
            int      outIndex, outLength;

            byte[] attachData;
            string text;

            Console.WriteLine("Extracting attachments...");

            do
            {
                if (reader.AttributeLevel != TnefAttributeLevel.Attachment)
                {
                    Assert.Fail("Expected attachment attribute level: {0}", reader.AttributeLevel);
                }

                switch (reader.AttributeTag)
                {
                case TnefAttributeTag.AttachRenderData:
                    attachment = new MimePart();
                    builder.Attachments.Add(attachment);
                    break;

                case TnefAttributeTag.Attachment:
                    if (attachment == null)
                    {
                        break;
                    }

                    while (prop.ReadNextProperty())
                    {
                        switch (prop.PropertyTag.Id)
                        {
                        case TnefPropertyId.AttachLongFilename:
                            attachment.FileName = prop.ReadValueAsString();

                            Console.WriteLine("Attachment Property: {0} = {1}", prop.PropertyTag.Id, attachment.FileName);
                            break;

                        case TnefPropertyId.AttachFilename:
                            if (attachment.FileName == null)
                            {
                                attachment.FileName = prop.ReadValueAsString();
                                Console.WriteLine("Attachment Property: {0} = {1}", prop.PropertyTag.Id, attachment.FileName);
                            }
                            else
                            {
                                Console.WriteLine("Attachment Property: {0} = {1}", prop.PropertyTag.Id, prop.ReadValueAsString());
                            }
                            break;

                        case TnefPropertyId.AttachContentLocation:
                            text = prop.ReadValueAsString();
                            if (Uri.IsWellFormedUriString(text, UriKind.Absolute))
                            {
                                attachment.ContentLocation = new Uri(text, UriKind.Absolute);
                            }
                            else if (Uri.IsWellFormedUriString(text, UriKind.Relative))
                            {
                                attachment.ContentLocation = new Uri(text, UriKind.Relative);
                            }
                            Console.WriteLine("Attachment Property: {0} = {1}", prop.PropertyTag.Id, text);
                            break;

                        case TnefPropertyId.AttachContentBase:
                            text = prop.ReadValueAsString();
                            attachment.ContentBase = new Uri(text, UriKind.Absolute);
                            Console.WriteLine("Attachment Property: {0} = {1}", prop.PropertyTag.Id, text);
                            break;

                        case TnefPropertyId.AttachContentId:
                            attachment.ContentId = prop.ReadValueAsString();
                            Console.WriteLine("Attachment Property: {0} = {1}", prop.PropertyTag.Id, attachment.ContentId);
                            break;

                        case TnefPropertyId.AttachDisposition:
                            text = prop.ReadValueAsString();
                            if (attachment.ContentDisposition == null)
                            {
                                attachment.ContentDisposition = new ContentDisposition(text);
                            }
                            else
                            {
                                attachment.ContentDisposition.Disposition = text;
                            }
                            Console.WriteLine("Attachment Property: {0} = {1}", prop.PropertyTag.Id, text);
                            break;

                        case TnefPropertyId.AttachData:
                            if (prop.IsEmbeddedMessage)
                            {
                                Console.WriteLine("Attachment Property: {0} is an EmbeddedMessage", prop.PropertyTag.Id);
                                var stream = prop.GetRawValueReadStream();
                                using (var tnef = new TnefReader(stream, reader.MessageCodepage, reader.ComplianceMode)) {
                                    var embedded = ExtractTnefMessage(tnef);
                                    Console.WriteLine("embedded attachments = {0}", embedded.BodyParts.Count());
                                    foreach (var part in embedded.BodyParts)
                                    {
                                        builder.Attachments.Add(part);
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("Attachment Property: {0} is not an EmbeddedMessage", prop.PropertyTag.Id);
                            }
                            break;

                        default:
                            Console.WriteLine("Attachment Property (unhandled): {0} = {1}", prop.PropertyTag.Id, prop.ReadValue());
                            break;
                        }
                    }
                    break;

                case TnefAttributeTag.AttachData:
                    if (attachment == null)
                    {
                        break;
                    }

                    attachData = prop.ReadValueAsBytes();
                    filter.Flush(attachData, 0, attachData.Length, out outIndex, out outLength);
                    attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
                    attachment.ContentObject           = new ContentObject(new MemoryStream(attachData, false));
                    filter.Reset();
                    break;

                default:
                    Console.WriteLine("Attachment Attribute (unhandled): {0} = {1}", reader.AttributeTag, prop.ReadValue());
                    break;
                }
            } while (reader.ReadNextAttribute());
        }
Exemple #12
0
        static void ExtractAttachments(TnefReader reader, BodyBuilder builder)
        {
            var      attachMethod = TnefAttachMethod.ByValue;
            var      filter = new BestEncodingFilter();
            var      prop = reader.TnefPropertyReader;
            MimePart attachment = null;
            int      outIndex, outLength;

            byte[] attachData;
            string text;

            do
            {
                if (reader.AttributeLevel != TnefAttributeLevel.Attachment)
                {
                    break;
                }

                switch (reader.AttributeTag)
                {
                case TnefAttributeTag.AttachRenderData:
                    attachMethod = TnefAttachMethod.ByValue;
                    attachment   = new MimePart();
                    break;

                case TnefAttributeTag.Attachment:
                    if (attachment == null)
                    {
                        break;
                    }

                    while (prop.ReadNextProperty())
                    {
                        switch (prop.PropertyTag.Id)
                        {
                        case TnefPropertyId.AttachLongFilename:
                            attachment.FileName = prop.ReadValueAsString();
                            break;

                        case TnefPropertyId.AttachFilename:
                            if (attachment.FileName == null)
                            {
                                attachment.FileName = prop.ReadValueAsString();
                            }
                            break;

                        case TnefPropertyId.AttachContentLocation:
                            text = prop.ReadValueAsString();
                            if (Uri.IsWellFormedUriString(text, UriKind.Absolute))
                            {
                                attachment.ContentLocation = new Uri(text, UriKind.Absolute);
                            }
                            else if (Uri.IsWellFormedUriString(text, UriKind.Relative))
                            {
                                attachment.ContentLocation = new Uri(text, UriKind.Relative);
                            }
                            break;

                        case TnefPropertyId.AttachContentBase:
                            text = prop.ReadValueAsString();
                            attachment.ContentBase = new Uri(text, UriKind.Absolute);
                            break;

                        case TnefPropertyId.AttachContentId:
                            attachment.ContentId = prop.ReadValueAsString();
                            break;

                        case TnefPropertyId.AttachDisposition:
                            text = prop.ReadValueAsString();
                            if (attachment.ContentDisposition == null)
                            {
                                attachment.ContentDisposition = new ContentDisposition(text);
                            }
                            else
                            {
                                attachment.ContentDisposition.Disposition = text;
                            }
                            break;

                        case TnefPropertyId.AttachData:
                            var stream  = prop.GetRawValueReadStream();
                            var content = new MemoryStream();
                            var guid    = new byte[16];

                            if (attachMethod == TnefAttachMethod.EmbeddedMessage)
                            {
                                var tnef = new TnefPart();

                                foreach (var param in attachment.ContentType.Parameters)
                                {
                                    tnef.ContentType.Parameters.Add(param.Name, param.Value);
                                }

                                if (attachment.ContentDisposition != null)
                                {
                                    tnef.ContentDisposition = attachment.ContentDisposition;
                                }

                                attachment = tnef;
                            }

                            // read the GUID
                            stream.Read(guid, 0, 16);

                            // the rest is content
                            stream.CopyTo(content, 4096);

#if !PORTABLE
                            var buffer = content.GetBuffer();
#else
                            var buffer = content.ToArray();
#endif
                            filter.Flush(buffer, 0, (int)content.Length, out outIndex, out outLength);
                            attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.SevenBit);
                            attachment.ContentObject           = new ContentObject(content);
                            filter.Reset();

                            builder.Attachments.Add(attachment);
                            break;

                        case TnefPropertyId.AttachMethod:
                            attachMethod = (TnefAttachMethod)prop.ReadValueAsInt32();
                            break;

                        case TnefPropertyId.AttachSize:
                            if (attachment.ContentDisposition == null)
                            {
                                attachment.ContentDisposition = new ContentDisposition();
                            }

                            attachment.ContentDisposition.Size = prop.ReadValueAsInt64();
                            break;

                        case TnefPropertyId.DisplayName:
                            attachment.ContentType.Name = prop.ReadValueAsString();
                            break;
                        }
                    }
                    break;

                case TnefAttributeTag.AttachCreateDate:
                    if (attachment != null)
                    {
                        if (attachment.ContentDisposition == null)
                        {
                            attachment.ContentDisposition = new ContentDisposition();
                        }

                        attachment.ContentDisposition.CreationDate = prop.ReadValueAsDateTime();
                    }
                    break;

                case TnefAttributeTag.AttachModifyDate:
                    if (attachment != null)
                    {
                        if (attachment.ContentDisposition == null)
                        {
                            attachment.ContentDisposition = new ContentDisposition();
                        }

                        attachment.ContentDisposition.ModificationDate = prop.ReadValueAsDateTime();
                    }
                    break;

                case TnefAttributeTag.AttachTitle:
                    if (attachment != null && string.IsNullOrEmpty(attachment.FileName))
                    {
                        attachment.FileName = prop.ReadValueAsString();
                    }
                    break;

                case TnefAttributeTag.AttachMetaFile:
                    if (attachment == null)
                    {
                        break;
                    }

                    // TODO: what to do with the meta data?
                    break;

                case TnefAttributeTag.AttachData:
                    if (attachment == null || attachMethod != TnefAttachMethod.ByValue)
                    {
                        break;
                    }

                    attachData = prop.ReadValueAsBytes();
                    filter.Flush(attachData, 0, attachData.Length, out outIndex, out outLength);
                    attachment.ContentTransferEncoding = filter.GetBestEncoding(EncodingConstraint.EightBit);
                    attachment.ContentObject           = new ContentObject(new MemoryStream(attachData, false));
                    filter.Reset();

                    builder.Attachments.Add(attachment);
                    break;
                }
            } while (reader.ReadNextAttribute());
        }