private void buttonSearch_Click(object sender, EventArgs e)
        {
            var rowsToDelete = new List <DataGridViewRow>();

            Task.Run(() =>
            {
                foreach (DataGridViewRow row in _parentForm.dataGridViewEmails.Rows)
                {
                    bool containsText = false;
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        var cellValue = cell.Value.ToString().ToLower();
                        if (cellValue != null)
                        {
                            if (cellValue.Contains(tbSearchText.Text.ToLower()))
                            {
                                containsText = true;
                            }
                        }
                    }

                    if (!containsText)
                    {
                        rowsToDelete.Add(row);
                    }
                }
                foreach (DataGridViewRow row in rowsToDelete)
                {
                    _parentForm.Invoke((Action)(() => _parentForm.dataGridViewEmails.Rows.RemoveAt(row.Index)));
                }
            });
        }
Beispiel #2
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            btnSend.Enabled           = false;
            btnSend.Text              = "Sending";
            pictureBoxLoading.Visible = true;
            Task.Run(() =>
            {
                try
                {
                    var mailSender = new MailSender(_parentForm.ServerInfo.SmtpServer, _parentForm.ServerInfo.SmtpPort
                                                    , true, _username, _password);
                    mailSender.Connect();
                    var sentEmail = mailSender.Send(tbFrom.Text, tbTo.Text, tbSubject.Text, tbBody.Text, Attachments);

                    mailSender.Disconnect();

                    _parentForm.MailReceiver.SentFolder.Append(sentEmail);
                }
                catch (Exception ex)
                {
                    _parentForm.Invoke((Action)(() => MessageBox.Show(ex.Message, "Sending failed")));
                    this.Invoke((Action)(() => pictureBoxLoading.Visible = false));
                }

                this.Invoke((Action)(() => pictureBoxLoading.Visible = false));
                this.Invoke((Action)(() => this.Close()));
            });
        }
        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;
            }
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            var serverInfo = new ServerInfo(ServerInfo.ServerPreset.Gmail);

            if (radioButtonChoosePreset.Checked)
            {
                if (comboBoxServer.Text == "Yandex")
                {
                    serverInfo = new ServerInfo(ServerInfo.ServerPreset.Yandex);
                }
            }
            else if (radioButtonCustomServer.Checked)
            {
                if (!Regex.IsMatch(textBoxImapPort.Text, @"^\d+$") || !Regex.IsMatch(textBoxSmtpPort.Text, @"^\d+$"))
                {
                    MessageBox.Show("IMAP and SMTP ports must be numberic value.", "Failed");
                    return;
                }

                serverInfo = new ServerInfo(textBoxImap.Text,
                                            Convert.ToInt32(textBoxImapPort.Text), textBoxSmtp.Text, Convert.ToInt32(textBoxSmtpPort.Text));
            }

            btnLogin.Enabled          = false;
            tbUsername.Enabled        = false;
            tbPassword.Enabled        = false;
            groupBox1.Enabled         = false;
            pictureBoxLoading.Visible = true;

            Task.Run(() =>
            {
                try
                {
                    var mailReceiver = new MailReceiver(serverInfo.ImapServer, serverInfo.ImapPort, true, tbUsername.Text, tbPassword.Text, _parentForm);
                    mailReceiver.Connect();

                    this.Invoke((Action)(() => this.Hide()));

                    if (!mailReceiver.Login.Contains("@"))
                    {
                        _parentForm.Invoke((Action)(() =>
                                                    _parentForm.toolStripStatusLabel.Text = "Logged In - " + serverInfo.LoginSuffix));
                    }
                    else
                    {
                        _parentForm.Invoke((Action)(() =>
                                                    _parentForm.toolStripStatusLabel.Text = "Logged In - " + mailReceiver.Login));
                    }

                    _parentForm.ServerInfo = serverInfo;
                    _parentForm.Invoke((Action)(() => _parentForm.panelLoading.Visible = true));

                    _parentForm.MailReceiver   = mailReceiver;
                    _parentForm.ReceivedEmails = mailReceiver.GetInboxEmailList();
                    _parentForm.SentEmails     = mailReceiver.GetSentEmailList();
                    _parentForm.LoadEmails(EmailView.Inbox);

                    this.Invoke((Action)(() => btnLogout.Enabled = true));


                    _parentForm.Invoke((Action)(() => _parentForm.SetEnableToolbarButtons(true)));
                    _parentForm.Invoke((Action)(() => _parentForm.panelLoading.Visible = false));
                    this.Invoke((Action)(() => pictureBoxLoading.Visible = false));
                }
                catch (Exception ex)
                {
                    this.Invoke((Action)(() => btnLogin.Enabled = true));
                    this.Invoke((Action)(() => tbUsername.Enabled = true));
                    this.Invoke((Action)(() => tbPassword.Enabled = true));
                    this.Invoke((Action)(() => groupBox1.Enabled = true));
                    this.Invoke((Action)(() => pictureBoxLoading.Visible = false));
                    this.Invoke((Action)(() => MessageBox.Show(ex.Message, "Login Failed")));
                    _parentForm.Invoke((Action)(() => _parentForm.panelLoading.Visible = false));
                }
            });
        }