public MailPreview(MailClientForm parentForm, Models.CollectionEmail collectionEmail)
        {
            InitializeComponent();

            _parentForm = parentForm;

            tbFrom.Text    = collectionEmail.From;
            tbTo.Text      = collectionEmail.To;
            tbSubject.Text = collectionEmail.Subject;
            tbBody.Text    = collectionEmail.TextBody;

            if (!string.IsNullOrEmpty(collectionEmail.HtmlBody))
            {
                webBrowserBody.DocumentText = collectionEmail.HtmlBody;
                webBrowserBody.Visible      = true;
            }
            else if (!string.IsNullOrEmpty(collectionEmail.TextBody))
            {
                tbBody.Text = collectionEmail.TextBody;
            }

            lbDate.Text = "Date: " + collectionEmail.Date;
        }
        public void ImportEmailsFromFile(EmailType destinationType, string path, string destinationFolderName = null)
        {
            switch (destinationType)
            {
            case EmailType.Inbox:
            {
                var inboxEmails = LoadMimeMessages(path);
                foreach (var email in inboxEmails)
                {
                    _parentForm.MailReceiver.AddMessageToFolder(EmailType.Inbox, email);
                }
                _parentForm.Invoke((Action)(() => _parentForm.toolStripButtonRefresh.PerformClick()));
            }
            break;

            case EmailType.SentEmails:
            {
                var sentEmails = LoadMimeMessages(path);
                foreach (var email in sentEmails)
                {
                    _parentForm.MailReceiver.AddMessageToFolder(EmailType.SentEmails, email);
                }
                _parentForm.Invoke((Action)(() => _parentForm.toolStripButtonRefresh.PerformClick()));
            }
            break;

            case EmailType.CollectionEmail:
            {
                var collection = LoadMimeMessages(path);

                foreach (var email in collection)
                {
                    if (_parentForm.EmailCollection.ContainsKey(email.MessageId))
                    {
                        MessageBox.Show("Item already exists in collection", "Import failed");
                    }
                    else
                    {
                        var selectedFolder = _parentForm.FolderList[destinationFolderName];
                        ++selectedFolder.ItemCount;

                        var fromAddresses = new List <string>();
                        foreach (var address in email.From)
                        {
                            fromAddresses.Add(((MailboxAddress)address).Address);
                        }

                        var toAddresses = new List <string>();
                        foreach (var address in email.To)
                        {
                            toAddresses.Add(((MailboxAddress)address).Address);
                        }

                        var collectionEmail = new Models.CollectionEmail
                        {
                            Id               = email.MessageId,
                            From             = string.Join(";", fromAddresses),
                            To               = string.Join(";", toAddresses),
                            Subject          = email.Subject,
                            TextBody         = email.TextBody,
                            HtmlBody         = email.HtmlBody,
                            Date             = email.Date.UtcDateTime,
                            CustomFolderName = destinationFolderName,
                        };

                        _parentForm.EmailCollection.TryAdd(collectionEmail.Id, collectionEmail);
                    }
                }
            }
            break;
            }
        }
        public void ImportEmailsFromServer(ServerInfo importFrom, EmailType sourceType,
                                           EmailType destinationType, string username, string password, string destinationFolderName = null)
        {
            MailReceiver mailReceiver = new MailReceiver(importFrom.ImapServer, importFrom.ImapPort,
                                                         true, username, password, _parentForm);

            try
            {
                mailReceiver.Connect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Login Failed");
                return;
            }

            List <MimeMessage> messages = new List <MimeMessage>();

            if (sourceType == EmailType.Inbox)
            {
                messages = mailReceiver.GetInboxMimeMessages();
            }
            else if (sourceType == EmailType.SentEmails)
            {
                messages = mailReceiver.GetSentMimeMessages();
            }

            switch (destinationType)
            {
            case EmailType.Inbox:
            {
                foreach (var email in messages)
                {
                    _parentForm.MailReceiver.AddMessageToFolder(EmailType.Inbox, email);
                }
                _parentForm.Invoke((Action)(() => _parentForm.toolStripButtonRefresh.PerformClick()));
            }
            break;

            case EmailType.SentEmails:
            {
                foreach (var email in messages)
                {
                    _parentForm.MailReceiver.AddMessageToFolder(EmailType.SentEmails, email);
                }
                _parentForm.Invoke((Action)(() => _parentForm.toolStripButtonRefresh.PerformClick()));
            }
            break;

            case EmailType.CollectionEmail:
            {
                foreach (var email in messages)
                {
                    if (_parentForm.EmailCollection.ContainsKey(email.MessageId))
                    {
                        MessageBox.Show("Item already exists in collection", "Import failed");
                    }
                    else
                    {
                        var selectedFolder = _parentForm.FolderList[destinationFolderName];
                        ++selectedFolder.ItemCount;

                        var fromAddresses = new List <string>();
                        foreach (var address in email.From)
                        {
                            fromAddresses.Add(((MailboxAddress)address).Address);
                        }

                        var toAddresses = new List <string>();
                        foreach (var address in email.To)
                        {
                            toAddresses.Add(((MailboxAddress)address).Address);
                        }

                        var collectionEmail = new Models.CollectionEmail
                        {
                            Id               = email.MessageId,
                            From             = string.Join(";", fromAddresses),
                            To               = string.Join(";", toAddresses),
                            Subject          = email.Subject,
                            TextBody         = email.TextBody,
                            HtmlBody         = email.HtmlBody,
                            Date             = email.Date.UtcDateTime,
                            CustomFolderName = destinationFolderName,
                        };

                        _parentForm.EmailCollection.TryAdd(collectionEmail.Id, collectionEmail);
                    }
                }
            }
            break;
            }
            mailReceiver.Disconnect();
        }