Esempio n. 1
0
        /// <summary>
        /// Encode a set of transmission parts into a stream suitable for
        /// transmission with or without Binary-Encoding Attachment.
        /// </summary>
        /// <param name="parts">The parts to encode.</param>
        /// <param name="contentType">The MIME Type of the TransmissionPartCollection contents.</param>
        /// <param name="withBinaryEncodingAttachment">if true, save all Attachments with binary-encoding type. </param>
        /// <returns>An OptimalEncodingResult with the stream and the name of the backing file store if there is one.</returns>
        EncodingResult OptimalEncode(ITransmissionPartCollection parts, out string contentType, bool withBinaryEncodingAttachment)
        {
            using (var mime = new Mime()) {
                mime.NewMultipartRelated();

                foreach (ITransmissionPart part in parts)
                {
                    var mimePart = new Mime();

                    mimePart.SetHeaderField("Content-ID", part.Id);

                    //TODO optimize call to get the bytes from the stream
                    byte[] data = null;
                    using (var partStream = part.CopyOfStream()) {
                        using (var sr = new BinaryReader(partStream)) {
                            data = sr.ReadBytes((int)partStream.Length);
                        }
                    }
                    //TODO determine if this is how we should handle it.
                    if (!part.MimeType.Equals(MimeTypeHelper.JdfMimeType) &&
                        !part.MimeType.Equals(MimeTypeHelper.JmfMimeType) &&
                        !part.MimeType.StartsWith("text/"))
                    {
                        //for text and jdf/jmf documents, do not base64 encode them.
                        mimePart.EncodingType = Mime.MimeEncoding.Base64;
                    }
                    else
                    {
                        mimePart.EncodingType = Mime.MimeEncoding.Binary;
                    }
                    mimePart.SetBodyFromBinary(data);

                    mimePart.ContentType = part.MimeType;
                    mime.AppendPart(mimePart);
                }

                contentType = mime.ContentType;

                mime.CreateDefaultType();

                //TODO see if there is a more optimal way to get the stream from the mime.
                return(new EncodingResult(mime.GetMimeStream(), contentType));
            }
        }