コード例 #1
0
        /// <summary>
        ///     Converts an EML file to MSG format
        /// </summary>
        /// <param name="emlFileName">The EML (MIME) file</param>
        /// <param name="msgFileName">The MSG file</param>
        public static void ConvertEmlToMsg(string emlFileName, string msgFileName)
        {
            var eml    = MimeMessage.Load(emlFileName);
            var sender = new Sender(string.Empty, string.Empty);

            if (eml.From.Count > 0)
            {
                var mailAddress = ((MailboxAddress)eml.From[0]);
                sender = new Sender(mailAddress.Address, mailAddress.Name);
            }

            var representing = new Representing(string.Empty, string.Empty);

            if (eml.ResentSender != null)
            {
                representing = new Representing(eml.ResentSender.Address, eml.ResentSender.Name);
            }

            var msg = new Email(sender, representing, eml.Subject)
            {
                SentOn            = eml.Date.UtcDateTime,
                InternetMessageId = eml.MessageId
            };

            switch (eml.Priority)
            {
            case MessagePriority.NonUrgent:
                msg.Priority = Enums.MessagePriority.PRIO_NONURGENT;
                break;

            case MessagePriority.Normal:
                msg.Priority = Enums.MessagePriority.PRIO_NORMAL;
                break;

            case MessagePriority.Urgent:
                msg.Priority = Enums.MessagePriority.PRIO_URGENT;
                break;
            }

            switch (eml.Importance)
            {
            case MessageImportance.Low:
                msg.Importance = Enums.MessageImportance.IMPORTANCE_LOW;
                break;

            case MessageImportance.Normal:
                msg.Importance = Enums.MessageImportance.IMPORTANCE_NORMAL;
                break;

            case MessageImportance.High:
                msg.Importance = Enums.MessageImportance.IMPORTANCE_HIGH;
                break;
            }

            foreach (var to in eml.To)
            {
                var mailAddress = ((MailboxAddress)to);
                msg.Recipients.AddTo(mailAddress.Address, mailAddress.Name);
            }

            foreach (var cc in eml.Cc)
            {
                var mailAddress = ((MailboxAddress)cc);
                msg.Recipients.AddCc(mailAddress.Address, mailAddress.Name);
            }

            foreach (var bcc in eml.Bcc)
            {
                var mailAddress = ((MailboxAddress)bcc);
                msg.Recipients.AddBcc(mailAddress.Address, mailAddress.Name);
            }

            using (var headerStream = new MemoryStream())
            {
                eml.Headers.WriteTo(headerStream);
                headerStream.Position       = 0;
                msg.TransportMessageHeaders = Encoding.ASCII.GetString(headerStream.ToArray());
            }

            msg.BodyHtml = eml.HtmlBody;
            msg.BodyText = eml.TextBody;

            var skipFirst = true;

            foreach (var bodyPart in eml.BodyParts)
            {
                // We always skip the first bodypart because that is normaly the html, text or rtf body
                if (skipFirst)
                {
                    skipFirst = false;
                    continue;
                }

                var attachmentStream = new MemoryStream();
                var fileName         = bodyPart.ContentType.Name;
                var extension        = string.Empty;

                if (bodyPart is MessagePart)
                {
                    var part = (MessagePart)bodyPart;
                    part.Message.WriteTo(attachmentStream);
                    if (part.Message != null)
                    {
                        fileName = part.Message.Subject;
                    }

                    extension = ".eml";
                }
                else if (bodyPart is MessageDispositionNotification)
                {
                    var part = (MessageDispositionNotification)bodyPart;
                    fileName = part.FileName;
                }
                else if (bodyPart is MessageDeliveryStatus)
                {
                    var part = (MessageDeliveryStatus)bodyPart;
                    fileName  = "details";
                    extension = ".txt";
                    part.WriteTo(FormatOptions.Default, attachmentStream, true);
                }
                else
                {
                    var part = (MimePart)bodyPart;
                    part.ContentObject.DecodeTo(attachmentStream);
                    fileName = part.FileName;
                    bodyPart.WriteTo(attachmentStream);
                }

                fileName = string.IsNullOrWhiteSpace(fileName)
                    ? "Nameless"
                    : FileManager.RemoveInvalidFileNameChars(fileName);

                if (!string.IsNullOrEmpty(extension))
                {
                    fileName += extension;
                }

                var inline = bodyPart.ContentDisposition != null &&
                             bodyPart.ContentDisposition.Disposition.Equals("inline",
                                                                            StringComparison.InvariantCultureIgnoreCase);

                attachmentStream.Position = 0;
                msg.Attachments.Add(attachmentStream, fileName, -1, inline, bodyPart.ContentId);
            }

            msg.Save(msgFileName);
        }