コード例 #1
0
        public static void ConverteToMsg(ApiEmail emails, List <Attachment> attac, string folderPath)
        {
            var email = new MsgKit.Email(new MsgKit.Sender(emails.Sender.EmailAddress, emails.Sender.Name), emails.Subject)
            {
                BodyHtml = emails.Html,
                BodyText = emails.Message,
                SentOn   = emails.Date,
            };
            User rec;

            for (int i = 0; i < emails.Recipient.Count; i++)
            {
                rec = emails.Recipient[i];
                email.Recipients.AddTo(rec.EmailAddress, rec.Name);
            }

            switch (emails.State)
            {
            case MessageState.Read:
                email.IconIndex = MsgKit.Enums.MessageIconIndex.ReadMail;
                break;

            case MessageState.Unread:
                email.IconIndex = MsgKit.Enums.MessageIconIndex.NewMail;
                break;

            case MessageState.Send:
                email.IconIndex = MsgKit.Enums.MessageIconIndex.ForwardedMail;
                break;

            case MessageState.None:
                break;
            }
            string filePath;

            foreach (var msgAtachment in attac)
            {
                filePath = msgAtachment.path;
                FileStream fstream = new FileStream(filePath + msgAtachment.namestr, FileMode.OpenOrCreate);
                email.Attachments.Add(fstream, msgAtachment.namestr);
            }
            using (var file = File.Open(Path.Combine(folderPath, Path.GetRandomFileName() + ".msg"), FileMode.CreateNew, FileAccess.Write))
                email.Save(file);
        }
コード例 #2
0
        /// <summary>
        /// converte message to eml format
        /// </summary>
        /// <param name="email"></param>
        public static void ConverteToEml(ApiEmail email, List <Attachment> attac, string folderPath)
        {
            Mime       emlmes     = new Mime();
            MimeEntity mainEntity = emlmes.MainEntity;

            mainEntity.From = new AddressList();
            mainEntity.From.Add(new MailboxAddress(email.Sender.Name, email.Sender.EmailAddress));
            mainEntity.To = new AddressList();
            mainEntity.To.Add(new MailboxAddress(email.Recipient[0].Name, email.Recipient[0].EmailAddress));
            mainEntity.Subject                 = email.Subject;
            mainEntity.ContentType             = MediaType_enum.Multipart_mixed;
            mainEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;

            MimeEntity textEntity = mainEntity.ChildEntities.Add();

            textEntity.ContentType             = MediaType_enum.Text_plain;
            textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
            textEntity.DataText = email.Message;

            MimeEntity htmlEntity = mainEntity.ChildEntities.Add();

            htmlEntity.ContentType             = MediaType_enum.Text_html;
            htmlEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
            htmlEntity.DataText = email.Html;

            mainEntity.Date        = email.Date.Value;
            mainEntity.MessageID   = GetEmailMetadata(email.message, "Message-ID");
            mainEntity.MimeVersion = GetEmailMetadata(email.message, "MIME-Version");
            if (attac.Count != 0)
            {
                foreach (var att in attac)
                {
                    MimeEntity attachmentEntity = mainEntity.ChildEntities.Add();
                    attachmentEntity.ContentTypeString       = att.mimeType;
                    attachmentEntity.ContentDisposition      = ContentDisposition_enum.Attachment;
                    attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
                    attachmentEntity.Data = att.name;
                    attachmentEntity.ContentDisposition_FileName = att.namestr;
                }
            }
            emlmes.ToFile(folderPath + "\\" + email.message.Id + ".eml");
        }